Team LiB
Previous Section Next Section

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. Let's see what you have.

  1. Open a new file in your text editor.

  2. Type the following line of PHP code:

    <? phpinfo(); ?>
    
  3. Save the file with the name phpinfo.php, and place this file in the document root of your web server.

  4. Open your web browser and type http://127.0.0.1/phpinfo.php.

Click To expand

You should see a very long page full of interesting information.

Note 

This information will differ, not only from machine to machine, 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 Apache Environment. In the next sections, you'll 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.

Retrieving and using REMOTE_ADDR

By default, environment variables are available to PHP scripts as $VAR_NAME. For example, the REMOTE_ADDR environment variable is already contained as $REMOTE_ADDR. However, to be absolutely sure that you're reading the correct value, you can use the getenv() function to assign a value to a variable of your choice.

The REMOTE_ADDR environment variable contains the IP address of the machine making the request. Let's get the value of your REMOTE_ADDR.

  1. Open a new file in your text editor.

  2. Open a PHP block, and then use getenv() to place the value of REMOTE_ADDR in a variable called $address:

    <?
    $address = getenv("REMOTE_ADDR");
    
  3. Print the value of $address to the screen, and close your PHP block:

    echo "Your IP address is $address.";
    ?>
    
  4. Save the file with the name remoteaddress.php, and then place this file in the document root of your web server.

  5. Open your web browser and type http://127.0.0.1/remoteaddress.php.

Click To expand

Your current IP address is printed to the screen.

Note 

Your IP address will differ from that shown here, which is my own IP at the moment I ran this script.

In the next section, you'll get the value of another handy environment variable, HTTP_USER_AGENT, which is the environment variable that holds the identifying string of the web browser being used.

Retrieving and using HTTP_USER_AGENT

The HTTP_USER_AGENT variable contains the browser type, browser version, language encoding, and platform. For example, the following value string refers to the Netscape browser, version 7.1, in English, on the Windows platform:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4)
Gecko/20030624 Netscape/7.1 (ax).

Here is another common HTTP_USER_AGENT value:

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

This value refers to Microsoft Internet Explorer (MSIE) version 6.0 on Windows. Sometimes you will see MSIE return an HTTP_USER_AGENT value that looks like a Netscape value, such as this one, which begins with Mozilla, until you notice that the value says it's compatible and is actually MSIE 6.0.

Finally, don't count out the text-only browsers! A Lynx HTTP_USER_AGENT value looks something like this:

Lynx/2.8rel.3 libwww-FM/2.14

Let's find your HTTP_USER_AGENT.

  1. Open a new file in your text editor.

  2. Open a PHP block, and then use getenv() to place the value of HTTP_USER_AGENT in a variable called $agent:

    <?
    $agent = getenv("HTTP_USER_AGENT");
    
  3. Print the value of $agent to the screen, and then close your PHP block:

    echo " You are using $agent.";
    ?>
    
  4. Save the file with the name useragent.php, and then place this file in the document root of your web server.

  5. Open your web browser and type http://127.0.0.1/useragent.php.

Click To expand

Your current HTTP_USER_AGENT value is printed to the screen.

Note 

Your user agent string might be different than the one shown, because each browser, version, and platform creates its own identification string.

In this chapter you've used several types of variables, including variables from forms. Variables are absolutely essential bits of your scripts, so becoming intimately familiar with them is a good thing. In the next chapter, you'll learn many of the basic tasks for web developers, including displaying dynamic content, sending e-mail, and working with your file system—all of which build on the use of variables.


Team LiB
Previous Section Next Section