Quick Tips

Have a MySQL DB you want to backup easily?  How about scheduling it from your desktop?

Check out MySQL Administrator in our MySQL Resource Links section.

 
Home arrow Code & Tutorials arrow Tutorials arrow Getting Started with PHP
Getting Started with PHP PDF Print

PHP for Aging Stoners - A Basic Guide to Getting Started

The title of this came from a joke posting on a forum a few years back and made it to this overview back then.  It's just a joke, so please don't read anything into it.

What is PHP? Well, it still does not stand for Pretty Hot Program.

PHP is a server side scripting language. What the heck does that mean? If you have any experience with something like Java Script or VB Script, they are client side scripting languages. One works on the client side and the other works on the server side. Sounds simple enough, right? It really is that simple of a concept.

Why use a server side scripting language like PHP? One reason is that you can better depend on your server settings than you can on the clients settings. Something like displaying the current date - who knows if the clients computer clock is right?! If you have a good host, you can be pretty sure the date is right on the server, so you might want to display that date rather than depend on the clients information. I say might, because you may have to think about timezone information when using the server time.

Another reason to use server side scripting rather than client side scripting is that you are serving ‘final’ content to your surfers. What ever the PHP dynamic content ends up being (no matter how many decisions get made in the process), the surfer only is served what is needed and nothing extra, so your page weight can be less. Say you wanted to have a random image on your site and select from 20 possible images. In JavaScript, this could be 30-40 lines of code and a management nightmare if you want to change the images around. If you did the same thing in PHP, the only HTML served for the random image selected would be one the image tag.

What is PHP best for? Creating dynamic content on a web site. This dynamic content may come from simply allowing you to make decisions on your site or from the ability to connect to a database. Something that you cannot do easily with a client side script is have random content on a site. It can be done, but it’s not easy to maintain. With PHP (and other server side languages) it’s not hard at all and maintenance can be minimized many times.

Here are two simple examples of how PHP can be helpful.

First Example...

Every directory should have an ‘index’ file so the contents cannot be accidentally accessed by Joe Surfer. Maybe it would be nice, rather than having a generic index file that tells people they are in the wrong place or depends on a client side script to redirect them, to have a universal index file that would always redirect surfers to your home page. With PHP, that’s easy to do.
This script will do exactly that for any site where the URL begins with ‘www.’ - just save this code to index.php and put it in every directory that needs an index file for security. Double check that your server can deal with ‘index.php’ as a proper index file.
<?PHP
// only purpose is to redirect traffic if it hits this page
// used as index.php in directories that need an index page
header("Location: );]http://www.\".$_SERVER['SERVER_NAME']);.
?>

The $_SERVER[‘SERVER_NAME’] is a superglobal variable that returns your_server_name.com or what ever your TLD may be. This kind of server side redirect is very fast since it happens on the server rather than the client side. That one line of code starting with ‘header’ is what actually does the redirect.

Second Example... 

Every site should have error pages configured that are specific to the site rather than using the hosts generic error pages. You can have a single error page for any site with PHP. If you don’t mind a plain error page without any graphics, the simple code below will put the basic information that a surfer might need and give them a way to get back to your main page. You can alter the message for each type of error page a surfer could get, but the same page can be used on any site without being altered. It will always show the right domain and contact information for that domain.

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>

<title>404 Error Page</title>
</head>
<body>
<?php print ("www.".$_SERVER['SERVER_NAME']); ?>
<hr>
The document you requested is not on our server.
404 Error
Please use this link to go to our <a href="http://www.<?php print ($_SERVER['SERVER_NAME']); ?>" target="_self">home page</a>.
If the problem persists, please inform our webmaster at <em>webmaster@<?php print ($_SERVER['SERVER_NAME']); ?></em>
</body>
</html>


There is a lot more that PHP can do for you, but this might give you some simple starting points. Anybody can start using PHP scripts just by taking time to read about them. Many scripts can be found on sites like hotscripts.com. If you want to start making your own scripts or alter ones that you have found, all you need is a basic understanding of programming to get started.

Some of our favorite PHP tutorials can be found at www.phpfreaks.com.

Enjoy!

 
< Prev   Next >
© 2010 Quiet Thunder Marketing
Joomla! is Free Software released under the GNU/GPL License.