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");
Why not use sizeof() within the random quotes? Using sizeof, you can simply add another quote and no need to care for the size below.
$randomnumber = rand (1,sizeof($randomquote));
You really should not recommend hashing passwords with md5 nowadays, since a simple google search CAN reveal the true password.
If you want to be on the safe side (for now!) go with sha1.
Also to build upon the DRY principle..:
$hashAlgorithm = ‘sha1’;
So you can simply change the algorithm (at least on the PHP side) by changing the string.
[…] this link: Handy PHP Tips and Tricks for Beginners | DesignBeep Share and […]
great article I’ll share this link with students
yeah that was very useful to me ……thank you……
[…] Eine kleine Sammlung von Tipps&Tricks für Einsteiger. […]
Handy PHP Tips and Tricks for Beginners…
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…
Also, instead of using double quote ie ” use the single quotes ‘ when referring to strings, this reduces the time needed for processing and allows the use of double quote inside of single for things such as HTML tags etc.
You are really out of date. 🙂
– Use microtime(true) from PHP 5.0.
– srand is useless after PHP 4.2.
– pure md5 is careless nowadays. Use sha1 with a salt
– don’t forget to set timezone on using date or you will run into problems with last php versions
– this will cause an error $username = ‘username’);
[…] Čítať ďalej originálny článok […]