In this tutorial, we would begin to see how you can use PHP to create dynamic content for the web.
- Identifying User Platform
- Display Images on Web Page
- Working With HTML Forms
- Redirecting the Browser
1. Identifying User Browser and Platform
Most information about the PHP environment are available as environment variables. All these can be seen in the phpinfo() page. One important environment variable is the HTTP_USER_AGENT which identifies the user’s browser as well as the user OS.
Also, there is a function called getenv() provided to access the value of the environment variables. The information contained in the HTTP_USER_AGENT can be read at runtime and used to output dynamic content to the user.
The code below shows how to retrieve the user’s computer using the environment variables. The function getPlatform() does the work.
<?php function getPlatform(){ $user_agent = $_SERVER["HTTP_USER_AGENT"]; $platform = "Unknown"; //Get the browser platform if(preg_match("/windows|win32/i", $user_agent)){ $platform = "Windows"; } elseif(preg_match("/linux/i", $user_agent)){ $platform = "Linux"; } elseif(preg_match("/macintosh|mac os x/i", $user_agent)){ $platform = "Mac"; } echo "You are using a " . $platform . " computer"; } getPlatform(); //Call the function ?>
On my pc, the code output the following text: “You are using a Mac computer“. For you it may be different depending on your pc.
2. A Program to Display Images
We would not write a program to display images on the browser. There a three functions you will learn here:
rand() – this function is used to generate random numbers. We would use it to generate random numbers within a given range
srand() – if you want to avoid getting the same pattern of random numbers, then srand() is better. So you then specify a seed.
microtime() – this is an inbuilt function used to return the current timestamp in microseconds
<?php srand(microtime() * 1000000); $val = rand(1, 4); switch($val) { case 1: $image_path = "/images/squirrel.jpg"; break; case 2: $image_path = "/images/dog.jpg"; break; case 3: $image_path = "/images/lion.jpg"; break; case 4: $image_path = "/images/horse.jpg"; break; } echo "Current Image is: <img src=$image_path />"; ?>
3. Working With HTML Forms
Let’s now see how we can create a form submission using PHP. Normally, form elements are placed in HTML pages. These form elements are available to the PHP script as well. An example is given below
<?php if($_POST["name"] || $_POST["score"]){ if(preg_match("/^A-Za-z'-]/", $_POST['name'])) { exit("The name you entered is not valid!"); } echo "Hello ".$_POST['name']. "<br>"; echo "You scored ". $_POST['score']. "<br>"; exit(); } ?> <html> <body> <form action="<?php $PHP_SELF ?>" method = "POST"> Name: <input type="text" name="name" /> <br> Age: <input type="text" name="score" /> <input type="submit"/> </form> </body> </html>
A little bit of explanation of the code above is provided here:
- the variable $_PHP_SELF is a PHP default variable and it means the the form is submitted to the same page
- the POST method is used to transfer the content of the form to the PHP script for processing
4. Redirecting the Browser
PHP provides a function called header() which is used to redirect the browser to another location while providing header attributes in raw format. The script for redirection should be placed at the top of the script. In this way, the page would be redirected before loading the current page.
The target location is specified as an argument to the header() function After call this function, the exit() function can then be used to prevent executing the rest of the code.
The example below shows how you can redirect the browser to another page. I recommend you try it out yourself to see how it works.
<?php if ($_POST["location"]) { $location = $_POST["location"]; //get location value from form header("Location:$location"); exit(); } ?> <html> <body> Select from the list of pages: <br> <form action="<?php $_SERVER['PHP_SELF'] ?>" method = "POST"> <select name="location">. <option value="https://www.kindsonthegenius.com"> kinsonthegenius.com </option> <option value="http://www.datarmatics.com"> Datarmatics site </option> </select> <input type="submit" /> </form> </body> </html>