Http Environment Variables

Http Environment Variables

HTTP ENVIRONMENT VARIABLES

When a web browser makes a request of a web server, it sends along with the request a list of extra variables. These are called environment variables, and they can be very useful for displaying dynamic content or authorizing users.

The phpinfo() function displays a wealth of information about your web server software and the version of PHP you are running - in addition to the basic HTTP environment. These include an environment variable called HTTP_USER_AGENT which identifies the user’s browser and operating system.

  1. Create a folder in your My PHP Sites folder called Environment Variables.
  1. Create a new Dreamweaver site pointing to this folder. You will need to add Remote Info and Testing Server information, and create a new folder on the server Environment_Variables to which you will publish.
  1. Open a new PHP file, and enter the following between the opening and closing <body> tags:

<? phpinfo(); ?>

  1. Save the file with the name phpinfo.php.

Your code should appear as shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<html xmlns="

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Environment Variables - phpinfo()</title>

</head>

<body>

<?php

phpinfo();

?>

</body>

</html>

  1. Publish your page and view the published page in your browser:

Note: This information will differ, not only from computer to computer, but also from platform to platform and version to version. Your results will vary, but the overall template is the same.

As you scroll down, look for a section titled PHP Variables. You will learn how to use two environment variables found here: REMOTE_ADDR and HTTP_USER_AGENT. For an explanation of some of the other HTTP environment variables shown in the phpinfo() output, visit http://hoohoo.ncsa.uiuc.edu/cgi/env.html.

By default, environment variables are available to PHP scripts as $VAR_NAME. For example, the REMOTE_ADDR environment variable is already contained in $REMOTE_ADDR.

The REMOTE_ADDR environment variable contains the IP address of the machine making the request.

One method of assessing this value is using the getenv() function to assign a value to a variable of your choice:

<?php

$ip = getenv(“REMOTE_ADDR”);

echo “IP Address:<br />$ip<br />”;

$viewer = getenv(“HTTP_USER_AGENT”);

echo “Browser Details:<br />$viewer”;

?>

We are going to use the predefined, superglobal variable $_SERVER to access server and execution environment information. $_SERVER is an array containing information about the server, the software that is running on it, the current script that produced the request, and about the client and its software. The server in communication with the client produces this information and stores it in the superglobal array $_SERVER. Each server produces a slightly different set of elements in the array – which are defined by their associative keys.

A related predefined variable which contains similar information is $HTTP_SERVER_VARS. However, $HTTP_SERVER_VARS is considered deprecated.

  1. Open a new PHP file, and enter the following between the opening and closing <body> tags:

<?php

$viewer = $_SERVER["HTTP_USER_AGENT"];

echo "Browser details:<br />$viewer<br /<br />";

$ip = $_SERVER['REMOTE_ADDR'];

echo "IP Address:<br />$ip<br /<br />";

$ip2 = $HTTP_SERVER_VARS["REMOTE_ADDR"];

echo "IP Address:<br />$ip2<br />";

?>

  1. Save the file with the name identify.php.

Your code should appear as shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<html xmlns="

<head>

<title>Identify Browser and Platform</title>

</head>

<body>

<?php

$viewer = $_SERVER["HTTP_USER_AGENT"];

echo "Browser details:<br />$viewer<br /<br />";

$ip = $_SERVER['REMOTE_ADDR'];

echo "IP Address:<br />$ip<br /<br />";

$ip2 = $HTTP_SERVER_VARS["REMOTE_ADDR"];

echo "IP Address:<br />$ip2<br />";

?>

</body>

</html>

  1. Publish your page and view the published page in your browser:

You are going to write a short script for displaying all the $_SERVER array elements on your server.

  1. Open a new PHP file, and enter the following between the opening and closing <body> tags:

<h1>Exploring Server Variables</h1>

<?php

foreach ($_SERVER as $key => $value) {

echo "<b> $key &nbsp : &nbsp </b> $value ", "<br />";

};

echo $_SERVER["HTTP_USER_AGENT"];

?>

  1. Save the file with the name list_server_variables.php.

Your code should appear as shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<html xmlns=" lang="en" xml:lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Listing 7-5</title>

</head>

<body>

<h1>Exploring Server Variables</h1>

<?php

foreach ($_SERVER as $key => $value) {

echo "<b> $key &nbsp : &nbsp </b> $value ", "<br />";

};

echo $_SERVER["HTTP_USER_AGENT"];

?>

</body>

</html>

  1. Publish your page and view the published page in your browser:

The information contained within the HTTP_USER_AGENT environment variable, for example, can be used to create dynamic content. – in this case, appropriate to the browser or platform.

In order to use server variables, you should use the particular key with $_SERVER. Since the value of most strings is a long and differing string, you do not want to check for equality – but rather check if the string contains a particular word.

The following script checks to see if the browser being used is either Internet Explorer or Firefox, and displays content depending on the browser:

  1. Open a new PHP file, and enter the following between the opening and closing <body> tags:

<h1>Checking for Browser Type</h1>

<?php

if(strpos($_SERVER["HTTP_USER_AGENT"], "MSIE")) {

echo "The browser is Internet Explorer. <br />";

}

elseif(strpos($_SERVER["HTTP_USER_AGENT"], "Firefox")) {

echo "The browser is Firefox. <br />";

}

else {

echo "A different browser is being used. <br />";

}

?>

  1. Save the file with the name detect_browser1.php.

Your code should appear as shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<html xmlns=" lang="en" xml:lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Detecting Browser Type</title>

</head>

<body>

<h1>Checking for Browser Type</h1>

<?php

if(strpos($_SERVER["HTTP_USER_AGENT"], "MSIE")) {

echo "The browser is Internet Explorer. <br />";

}

elseif(strpos($_SERVER["HTTP_USER_AGENT"], "Firefox")) {

echo "The browser is Firefox. <br />";

}

else {

echo "A different browser is being used. <br />";

}

?>

</body>

</html>

  1. Publish your page and view the published page in a range of browsers:

The strpos() function is both powerful and interesting. Its stated purpose is to find the position of the first occurrence of one string in another string. It is most frequently used, though, to simply find if one string exists in another.

The next example uses the preg_match() function which seeks to match a specified string pattern:

preg_match("/string-to-seek/", "string-to-search");

The script tries to identify both browser and platform. Default strings are provided when no match is found. The appropriate string is then written to the page.

Note: The I switch is included in the preg_match() first argument to ensure a case-insensitive search.

  1. Open a new PHP file, and enter the following between the opening and closing <body> tags:

<?php

$viewer = $_SERVER["HTTP_USER_AGENT"];

$browser = "an unidentified browser";

if(preg_match("/MSIE/i","$viewer"))

{$browser = "Internet Explorer";}

else if(preg_match("/Netscape/i","$viewer"))

{$browser = "Netscape";}

else if(preg_match("/Opera/i","$viewer"))

{$browser = "Opera";}

$platform = "an unidentified operating system";

if(preg_match("/Windows/i","$viewer"))

{$platform = "Windows";}

else if(preg_match("/Linux/i","$viewer"))

{$platform = "Linux";}

echo("You're using $browser on $platform");

?>

  1. Save the file with the name detect_browser2.php.

Your code should appear as shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<html xmlns="

<head>

<title>Detecting Browser Type</title>

</head>

<body>

<?php

$viewer = $_SERVER["HTTP_USER_AGENT"];

$browser = "an unidentified browser";

if(preg_match("/MSIE/i","$viewer"))

{$browser = "Internet Explorer";}

else if(preg_match("/Netscape/i","$viewer"))

{$browser = "Netscape";}

else if(preg_match("/Opera/i","$viewer"))

{$browser = "Opera";}

$platform = "an unidentified operating system";

if(preg_match("/Windows/i","$viewer"))

{$platform = "Windows";}

else if(preg_match("/Linux/i","$viewer"))

{$platform = "Linux";}

echo("You're using $browser on $platform");

?>

</body>

</html>

  1. Publish your page and view the published page in a range of browsers:

Logging Visitor Details and Saving to a Text File

The ability to write files makes it simple for PHP to log details about visitors to a web page.

It is especially interesting to discover how visitors arrived at the page by recording the values of the environment variable HTTP_REFERER. This holds the URL of the page containing the hyperlink that the user followed to get to your page.

The frequency with which visitors return can be discovered by recording the IP addresses of visitors to a page. This is stored in the environment variable REMOTE_ADDR.

The following script demonstrates how to log these visitor details, along with their browser type and the time at which they accessed the page.

  1. Open a new PHP file, and enter the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<?php

error_reporting(E_ALL ^ E_NOTICE);

$address = $_SERVER['REMOTE_ADDR'];

$referer = $_SERVER['HTTP_REFERER'];

$browser = $_SERVER['HTTP_USER_AGENT'];

$filename = "C:/inetpub/wwwroot/users/btec3/student100/Environmental_Variables/logfile.txt";

$log_file = fopen($filename, "a+") or die("Couldn't open file");

$time = date("H:i dS F");

fwrite($log_file, "<b>Time:</b> $time<br />");

//write the user's IP address if available

if( $address != null)

{

fwrite($log_file, "<b>IP Address:</b> $address <br />");

}

//write the URL of the forwarding page if available

if($referer != null)

{

fwrite($log_file, "<b>Referer:</b> $referer<br />");

}

//write the user's browser details

fwrite($log_file, "<b>Browser:</b> $browser<br /<hr />");

fclose($log_file);

$whattoread = @fopen($filename, "r") or die("Couldn't open file");

$file_contents = fread($whattoread, filesize($filename));

$msg = "<br />$file_contents";

fclose($whattoread);

?>

<html xmlns="

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Writing Environmental Variables to a Log File and Reading from the Log File</title>

</head>

<body>

<?php

echo "<p>The following environmental variables have been written to the log file: </p>";

echo $msg;

?>

</body>

</html>

  1. Save the file with the name ev_log.php.
  1. Publish your page and view the published page in a range of browsers:
  1. Download a copy of logfile.txt from the server and examine its contents:

`

  1. You should now experiment by change the file type from logfile.txt to logfile.html.
  1. Create a link at the bottom of ev_log.php which links to logfile.html.
  1. Republish ev_log.php and view the published page in your browser.
  1. Check that the link to logfile.html works and details are displayed as expected.

Note: To find the complete list of server variables and their meaning, go to

A - Introduction to PHP and MySQL - Environment Variables.docVersion 1

Page 1 of 17