The World Wide Web is an ever evolving place where new technologies seem to disappear just as quickly as they arrived. In terms of building websites the kind of technologies we’re talking about are the languages used to create our online blogs, stores and forums.
Don’t know what server-side scripting is? Don’t worry. PHP, just like Perl and the many other scripting languages out there, is used to create truly interactive websites which, in conjunction with a good database, can store meaningful information. For example, if you have ever wondered how the registration system works on your favourite websites then you can think of it like this:
The user fills in the registration form with their details, such as a username and password. When the user clicks the submit button, all those details are sent to the server where they can then be analysed and stored, and that is where server-side scripting comes into play. Without a script file written in either PHP or any other language those details will simply be discarded like they never existed.
Now, to give you a taste of what PHP can do, and how it can help you build your website, we’re going to write some code that will help your web pages remember the options a user selects. So here is a scenario to think about: Imagine you give your visitors the option to change the background colour of your web pages, and you want the option they pick to remain until they either change it themselves or leave your website. The main problem here is how do you tell your web pages to remember such a thing.
It is surprisingly easy and takes only a few lines of code, so lets get our hands dirty and do some coding. First things first, create a blank PHP page, this should end with the .php extension and not txt or anything else. To keep things simple give the file the name testpage.php. Insert the following code into the page:
- //check if our post variable color has been set
- $_SESSION[‘color’] = $_POST[‘newcolor’];
- $_SESSION[‘color’] = “white”;
- <title> Test PHP Page </title>
- <body style=”background-color:<?php echo $_SESSION[‘color’]; ?> >
- <form method=”post” action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” >
- <option name=”newcolor”>
- <select value=”red”>Red</select>
- <select value=”green”>Green</select>
- <select value=”blue”>Blue</select>
- <input type=”submit” value=”submit” />
Now you may be thinking that there is a lot more than just 20 lines of code there, but in actual fact our PHP code is only about 12 lines and that’s including a comment. The rest is simple HTML markup which you should already be familiar with at this point.
So what does it all mean? Well, lets start at the top and work our way down. On lines 1 to 10 we use PHP open and close tags to tell the server what lays in-between is PHP code and should therefore be treated as such.
On line 2 we use a special built-in function of PHP called session_start and it is important to remember that if you want to work with sessions using PHP this function must always be called first before any other code on your page or it will simply not work.
On lines 4 to 9 we use an if-else statement to check if the $_POST array contains a variable called color, and if so does it have a value. If the condition is true then we assign the value of the $_POST[‘newcolor’] array element to PHP’s $_SESSION array. The $_SESSION array can be assigned any number of elements with a corresponding value so in our case we create a new element called ‘color’ and assign it the value posted to the server from our form.
If the $_POST[‘newcolor’] array element does not exist or is not set with a value, then we assign a default color of white to the $_SESSION array. This is also the value that will be assigned the very first time you load the page since you have not used the form yet to set a colour.
Now that we have a value to work with in our session array it’s actually straight forward enough to make use of it as you can see on line 17 where we simply echo out its value as part of the style attribute.
Obviously, for the sake of simplicity this has been a pretty simple scenario and probably one that you’re not likely to use much, but it does demonstrate the power of PHP’s session handling abilities and how you could use it to transport data input from your users between pages until you are ready to store it permanently.