Published on Apr 22,2015
2.8K Views
Email Post

In the previous post we saw the various built-in functions supported in PHP and how to pass the parameters to functions. In this PHP tutorial post, we will talk about the ways to identify the browser, how to generate random numbers and images and how to direct yourself to a particular website.

Identifying Browser & Platform:

Using the environment variables in PHP, we can identify the user’s browser. Environment Variables are the variables that affect the running processes in the system. These environment variables are present in the phpinfo.php page. HTTP_USER_AGENT is the variable used to identify user’s agent. To access the value of the environment variable HTTP_USER_AGENT we can use getenv() function.

Example:

<?php
$browser = getenv( "HTTP_USER_AGENT" );
echo $browser;
?>
Output:

The output depends on the browser which we are executing this script. It outputs our browser and operating system which we are using.

Random Generation:

Using PHP rand() function, we can generate a random number. This function is used to display anything random in browsers. Rand() returns a random number from zero to getrandmax(). You can also specify the range between which you expect the rand() to display the output like rand(10,20);

Example:

<?php
srand( microtime() * 1000000 );
$random_nos = rand( 5, 10 );
echo $ random_nos;
?>

Output:

The output will not be constant and will vary, but the output values will be 6 or 7 or 8 or 9.

Display Images Randomly:

Here a random image is displayed on the web. The images must be selected from the repository of the images stored on your system but the url of the image must be correct when writing the script for the same.

Example:

 <?php
srand( microtime() * 1000000 );
$image_no = rand( 7, 10 );
Echo “Image Number:” $ image_no;
switch( $ image_no )
{
case 7: $image_file = sunset.jpg"; break;
case 8: $image_file = “winter.jpg"; break;
case 9: $image_file = “summer.jpg"; break;
case 10: $image_file = “hills.jpg"; break;
}
 echo "Random Image : <img src=$image_file />";
?>

Output:

The output will be any of the four images depending on the random number generation.

Browser Redirection:

Browser Redirection is done so that the users are directed quickly and seamlessly to the website mentioned in the script, even when the link to that page is no longer available. In PHP the header() function is used to redirect to another location. And the redirection script should always be specified in the top of the page.

Example:

<?php header('Location: http://www.google.com/');
 exit;
?>

Output:

The above script will redirect to the Google page.

Sometimes it becomes necessary to add contents from one PHP script to another. This feature in PHP is provisioned using the include() function and require() function. These two functions in PHP are called as File Inclusion functions.Include() and require() functions will be discussed in detail in the next post.

Got a question for us? Please mention them in the comments section and we will get back to you.

Related Posts:

Get Started with PHP & MySQL

Differentiate PHP Code in HTML Script.

Functions in PHP

Share on

Browse Categories

Comments
2 Comments