News, Insights and Stories from the Australian and New Zealand tech ecosystem.

20 lines of code to help build your website

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.

Although you may not be overly familiar with all of them, the odds are pretty good that you have at least heard of some of them such as HTML, JavaScript and CSS. Now, in this article we’re going to be looking at the heavy-weight of the server-side scripting world: PHP.

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:

  1.  <?php
  2.      $session_start();
  3.      //check if our post variable color has been set
  4.      if(isset($_POST[‘color’]))
  5.      {
  6.           $_SESSION[‘color’] = $_POST[‘newcolor’];
  7.      }else{
  8.           $_SESSION[‘color’] = “white”;
  9.      }
  10. ?>
  11. <html>
  12. <head>
  13. <title> Test PHP Page </title>
  14. </head>
  15. <body style=”background-color:<?php echo      $_SESSION[‘color’]; ?> >
  16. <form method=”post” action=”<?php echo   $_SERVER[‘PHP_SELF’]; ?>” >
  17.      <option name=”newcolor”>
  18.           <select value=”red”>Red</select>
  19.           <select value=”green”>Green</select>
  20.           <select value=”blue”>Blue</select>
  21.      </option>
  22.      <input type=”submit” value=”submit” />
  23. </form>
  24. </body>
  25. </html>

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.

Startup Daily