Dynamic websites are slowly making their way toward innovating the visitor experience. With Web 2.0 holding PHP (PHP: Hypertext Preprocessor) as one of the most effective server-side scripting languages to accommodate these standards, you can transform your website’s content from a static form to a more user friendly one.
Famous websites such as WordPress and Facebook all use PHP as their backbone, making the language a really powerful one to use. If you’re starting out, you can check out these tips that will enhance your website by adding a little flavor to your design.
Showing Load Time
This is an integral goal for any web developer or system administrator. You need to know how fast your content loads. When your website takes too long to load, it could very well disappoint visitors and you would then lose traffic. This code snippet can help you check out the loading time of your document.
Insert this portion of the code within the tags:
<?php $begintime = microtime(); $beginarray = explode (" ", $begintime); $begintime = $beginarray + $beginarray; ?>
Then directly before your tag, insert this one:
<?php $endtime = microtime(); $endarray = explode (" ", $endtime); $endtime = $endarray + $endarray; $elapsedtime = $endtime - $begintime; $elapsedtime = round ($elapsedtime, 10); echo "Seconds to load website: "; printf ("%f", $elapsedtime); ?>
For those moments where you want to leave inspirational quotes for your visitors, this randomized quotes script done by PHP can conveniently carry out that task, which adds a great dynamic functionality to your website.
Insert this code anywhere on your website where you wish to show the quote:
<?php $randomquote = array ( 1 => "Quote 1", 2 => "Quote 2", 3 => "Quote 3" ); srand ((double) microtime() * 1000000); $randomnumber = rand (1,3); echo "$randomquote[$randomnumber]"; ?>
Password Protection Security with Encryption Measures
Password protection is high on web developers’ priority lists. MD5 encryption is available to use when encrypting passwords for database use. You should take note that MD5 is just a simple encryption measure. When you get more familiar with PHP, you can engage in other encryption formats.
Insert the code where you want to encrypt your password:
<?php echo md5('password'); ?>
Customized Comments System
Personalizing your website through a comments system can be very helpful for your visitor traffic, especially as it allows interaction in a dynamic way. You can use WordPress or any content management system to integrate in your website, but if you want to hard code your own, check out this reference at W3School on PHP forms and User Input.
Showing the current date on your website is a functional way of using PHP and is very convenient if you have news websites. Also, its operational value makes it a highly dynamic way of showing the current day, especially since some operating systems don’t show the current date at a glance of an eye.
Insert this piece of code anywhere where you want your date to appear:
<?php $currentdate = Date ("F d, Y"); echo "Today is " . $currentdate; ?>
Website Referred From
How a user reaches your website can be useful information. Also, if you couple it with storage code, you can record referrals so you know how people access your website. For now, this is how you can show which websites people come from upon reaching yours.
Insert this wherever you wish this to appear:
<?php $refer = $_SERVER['HTTP_REFERER']; echo "You reached this website through ". $refer; ?>
Daily Mood in Design
As a treat for your visitors, you can adjust your website’s “mood” through different background colors! This fun PHP snippet will allow you to also show images on certain days or whatever you want to offer as a surprise treat for your visitors.
Insert this portion in the part of your document:
<?php $currentday = date("w"); $color = array ("blue", "yellow", "pink", "green", "purple", "black", "red"); ?>
And in the container or element, insert this:
<?php print("$color[$currentday];"); ?>
<div style="background-color:<?php print $color[$currentday]; ?>;"> Content here </div>
Applying the DRY Principle
DRY or Don’t Repeat Yourself, is very important no matter what programming language you’re using. It involves placing data you’d otherwise use over and over again into variables so you can simply edit it from there. Some examples are like usernames, passwords and Boolean, which you may tweak later on.
Here’s what your code would look like without the DRY principle:
$mysql = mysql_connect('localhost', 'username', 'password'); mysql_select_db('wordpress') or die("Cannot find Database");
With the DRY principle implemented:
$hostname = 'localhost'; $username = 'username'); $password = 'password'; $database = 'wordpress'; $mysql = mysql_connect($hostname, $username, $password); mysql_select_db ($database) or die ("Cannot Find Database");