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.
<?php phpinfo(); ?>
Save the file with the name phpinfo.php and place this file in the
document root of your Web server.
Open your Web browser and type http://127.0.0.1/phpinfo.php
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, 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.
Open a new file in your text editor.
Open a PHP block, then use getenv to place the value of REMOTE_ADDR
in a variable called $address:
<?
$address = getenv("REMOTE_ADDR");
//Print the value of $address to the screen.
echo "Your IP address is $address.";
?>
Save the file with the name testaddress.php, then place this file in the
document root of your Web server.
Open your Web browser and type http://127.0.0.1/testaddress.php
PHP-HTTP_USER_AGENT
<?
$agent = getenv("HTTP_USER_AGENT");
Print the value of $agent to the screen,
echo " You are using $agent.";
?>
HTTP Headers
An HTTP - HyperText Transfer Protocol header is used to send information
back and forth between the server and the client the Web browser.
Normally this information is in the form of HTML, which is why
the address for Web pages begins with http://.
To redirect the user's browser with PHP, the code:
header("Location: page.php");
You can also use the header function to send cookies, which is a
good backup to the setcookie function which sometimes has
inconsistent results from one browser to the next.
But HTTP headers are a complicated enough subject to warrant a little more
attention. There are actually dozens upon dozens of uses for HTTP headers,
all of which you can take advantage of using PHP's header function.