PHP - Web Concepts: Identifying Browser & Platform
PHP - Web Concepts: Identifying Browser & Platform
This session demonstrates how PHP can provide dynamic content according to browser type,
randomly generated numbers or User Input. It also demonstrated how the client browser can be
redirected.
PHP creates some useful environment variables that can be seen in the phpinfo.php page that
was used to setup the PHP environment.
One of the environment variables set by PHP is HTTP_USER_AGENT which identifies the
user's browser and operating system.
PHP provides a function getenv() to access the value of all the environment variables. The
information contained in the HTTP_USER_AGENT environment variable can be used to create
dynamic content appropriate to the browser.
Following example demonstrates how you can identify a client browser and operating system.
<html>
<body>
<?php
function getBrowser() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version = "";
// Next get the name of the useragent yes seperately and for good
reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',
$u_agent)) {
$bname = 'Internet Explorer';
$ub = "MSIE";
} elseif(preg_match('/Firefox/i',$u_agent)) {
$bname = 'Mozilla Firefox';
$ub = "Firefox";
} elseif(preg_match('/Chrome/i',$u_agent)) {
$bname = 'Google Chrome';
$ub = "Chrome";
}elseif(preg_match('/Safari/i',$u_agent)) {
$bname = 'Apple Safari';
$ub = "Safari";
}elseif(preg_match('/Opera/i',$u_agent)) {
$bname = 'Opera';
$ub = "Opera";
}elseif(preg_match('/Netscape/i',$u_agent)) {
$bname = 'Netscape';
$ub = "Netscape";
}
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
// now try it
$ua = getBrowser();
$yourbrowser = "Your browser: " . $ua['name'] . " " .
$ua['version'] .
" on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];
print_r($yourbrowser);
?>
</body>
</html>
This is producing following result on my machine. This result may be different for your
computer depending on what you are using.
The PHP rand() function is used to generate a random number.i This function can generate
numbers with-in a given range. The random number generator should be seeded to prevent a
regular pattern of numbers being generated. This is achieved using the srand() function that
specifies the seed number as its argument.
Following example demonstrates how you can display different image each time out of four
images −
Live Demo
<html>
<body>
<?php
srand( microtime() * 1000000 );
$num = rand( 1, 4 );
switch( $num ) {
case 1: $image_file = "/php/images/logo.png";
break;
</body>
</html>
It will produce the following result −
The most important thing to notice when dealing with HTML forms and PHP is that any form
element in an HTML page will automatically be available to your PHP scripts.
Try out following example by putting the source code in test.php script.
<?php
if( $_POST["name"] || $_POST["age"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
exit();
}
?>
<html>
<body>
</body>
</html>
The PHP default variable $_PHP_SELF is used for the PHP script name and when you
click "submit" button then same PHP script will be called and will produce following
result −
The method = "POST" is used to post user data to the server script. There are two
methods of posting data to the server script which are discussed in PHP GET & POST
chapter.
Browser Redirection
The PHP header() function supplies raw HTTP headers to the browser and can be used to
redirect it to another location. The redirection script should be at the very top of the page to
prevent any other part of the page from loading.
The target is specified by the Location: header as the argument to the header() function. After
calling this function the exit() function can be used to halt parsing of rest of the code.
Following example demonstrates how you can redirect a browser request to another web page.
Try out this example by putting the source code in test.php script.
<?php
if( $_POST["location"] ) {
$location = $_POST["location"];
header( "Location:$location" );
exit();
}
?>
<html>
<body>
</select>
<input type = "submit" />
</form>
</body>
</html>