Using $_ENV and $_SERVER
Before you get control in your script, PHP sets several variables for you containing information about the server, the environment, and your visitor's request. These are stored in the superglobal arrays $_ENV and $_SERVER, but their availability depends on whether the script is being run through a web server or on the command line.
The most commonly used $_SERVER variables are shown in Table 5-3. Note: of these, only PHP_SELF is available on the command line.
Table 5-3. Useful preset variables in the $_SERVER superglobal
|
Name |
Value |
|---|---|
|
|
If the user clicked a link to get the current page, this will contain the URL of the previous page, or it will be empty if the user entered the URL directly. |
|
|
The name reported by the visitor's web browser. |
|
|
Any data passed in the URL after the script name. |
|
|
The name of the current script. |
|
|
Either GET or POST. |
|
|
Includes everything after the question mark in a GET request. Not available on the command line. |
Warning
You need to use HTTP_REFERER
and not HTTP_REFERRER. This is one of the few misspellings ever to make it into a web standard, but it's now in widespread use and too late to change.
Of those, HTTP_REFERER and HTTP_USER_AGENT
are the most important, as you can use these two to find out a lot about your visitor and then take the appropriate action. For example:
<?php if (isset($_SERVER['HTTP_REFERER'])) { print "The page you were ...