Web Concepts in PHP | Explanation with example
Tutorialscan in This session guide you about the Web Concepts in PHP.
How PHP can provide the dynamic content according to browser type, user Input or Randomly generated numbers.
Therefore also guide about how the client browser can be redirected.
Recognize Platform And Browser
In File phpinfo.php we can see the PHP Create some useful environment variables that were used to set up the PHP environment.
HTTP_USER_AGENT environment variables set by PHP which identify the operating system and user’s browser.
PHP Function getenv() to access the value environment variables and all information gathered in the HTTP_USER_AGENT.
So environment variable we can be used to create dynamic content for the browser.
First of all, let’s see with the example how we can identify a client browser and operating system
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<html> <body> <?php function getBrowser() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $browser_name = 'Unknown'; $platform = 'Unknown'; $version = ""; //First of all get the platform? if (preg_match('/linux/i', $user_agent)) { $platform = 'linux'; }elseif (preg_match('/macintosh|mac os x/i', $user_agent)) { $platform = 'mac'; }elseif (preg_match('/windows|win32/i', $user_agent)) { $platform = 'windows'; } // Next get the name of the useragent if(preg_match('/MSIE/i',$user_agent) && !preg_match('/Opera/i',$user_agent)) { $browser_name = 'Internet Explorer'; $ub = "MSIE"; } elseif(preg_match('/Chrome/i',$user_agent)) { $browser_name = 'Google Chrome'; $ub = "Firefox"; } elseif(preg_match('/Firefox/i',$user_agent)) { $browser_name = 'Mozilla Firefox'; $ub = "Chrome"; }elseif(preg_match('/Safari/i',$user_agent)) { $browser_name = 'Apple Safari'; $ub = "Safari"; }elseif(preg_match('/Opera/i',$user_agent)) { $browser_name = 'Opera'; $ub = "Opera"; }elseif(preg_match('/Netscape/i',$user_agent)) { $browser_name = 'Netscape'; $ub = "Netscape"; } // finally get the correct version number $known = array('Version', $ub, 'other'); $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#'; if (!preg_match_all($pattern, $user_agent, $matches)) { // we have no matching number just continue } // see how many we have $i = count($matches['browser']); if ($i != 1) { //we will have two since we are not using 'other' argument yet //see if version is before or after the name if (strripos($user_agent,"Version") < strripos($user_agent,$ub)){ $version= $matches['version'][0]; }else { $version= $matches['version'][1]; } }else { $version= $matches['version'][0]; } // check if we have a number if ($version == null || $version == "") {$version = "?";} return array( 'userAgent' => $user_agent, 'name' => $browser_name, 'version' => $version, 'platform' => $platform, 'pattern' => $pattern ); } // now try it $ua = getBrowser(); $yourbrowser = "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent']; print_r($yourbrowser); ?> </body> </html> |
Finally, It will produce the following result −</3>
1 2 3 |
Your browser: firefox 54.0.2840.99 on windows reports: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) firefoxe/54.0.2840.99 Safari/537.36 |
This result is producing on my machine. This result may be different according to your computer (machine) depending on what you are using the browser, Window, version.
HTML Forms
Important thing is that to notice PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
See the blew example by putting the source code in the form.php script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php if( $_POST["first_name"] || $_POST["email"] ) { if (preg_match("/[^A-Za-z'-]/",$_POST['first_name'] )) { die ("invalid name & name should be alpha"); } echo "Welcome ". $_POST['first_name']. "<br />"; echo "You are ". $_POST['email']. " years old."; exit(); } ?> <html> <body> <form action = "<?php $_PHP_SELF ?>" method = "POST"> First Name: <input type = "text" name = "Firstname" /> Email: <input type = "text" name = "email" /> <input type = "submit" /> </form> </body> </html> |
Finally, Output will have come:-
Conclusion
Variable $_PHP_SELF is used for PHP Script when you click submit button then same PHP Script will be called and it will us the result
“POST” Method is used to post user data to the server script, Here we can USE “GET” Method, because of PHP GET and POST Method are used to posting data to the server script.
Randomly Images Display
Here we need to generate random number so we can use PHP rand() function, this function generates numbers within a given range.
the PHP function srand() will be used that specifies the seed number as its argument
See THE Following given example to display images each time-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<html> <body> <?php srand( microtime() * 999999 ); $num = rand( 1, 3 ); switch( $num ) { case 1: $image_file = "/php/images/slider1.png"; break; case 2: $image_file = "/php/images/slider2.jpg"; break; case 3: $image_file = "/php/images/slider3.png"; break; case 4: $image_file = "/php/images/slider4.jpg"; break; case 5: $image_file = "/php/images/slider5.jpg"; break; } echo "Random Image : <img src=$image_file />"; ?> </body> </html> |
Finally, the result will come −
Browser Redirection
In PHP web concepts in Browser Redirection PHP header() function supplies raw HTTP headers to the browser and it can be used to redirect to another location.
The header as the argument to the header function, the target is specified by the Location, After calling the function the exit() function can be used.
One thing noticed here the redirection script should be at the top of the page to prevent any other page from loading
Example: testrun.php (putting the source code in this testrun.php script)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php if( $_POST["location"] ) { $location = $_POST["location"]; header( "Location:$location" ); exit(); } ?> <html> <body> <p>Choose a website to visit :</p> <form action = "<?php $_SERVER['PHP_SELF'] ?>" method ="POST"> <select name = "location">. <option value = "http://www.tutorialscan.com"> Tutorialscan.com </option> <option value = "http://www.gmail.com"> Google email page </option> </select> <input type = "submit" /> </form> </body> </html> |
Finally output-
The following above example you can use to redirect browser request to another web page.