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.
Name
|
Value
|
---|---|
HTTP_REFERER
|
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.
|
HTTP_USER_AGENT
|
The name reported by the visitor's web browser.
|
PATH_INFO
|
Any data passed in the URL after the script name.
|
PHP_SELF
|
The name of the current script.
|
REQUEST_METHOD
|
Either GET or POST.
|
QUERY_STRING
|
Includes everything after the question mark in a GET request.
Not available on the command line.
|
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 "previously was {$_SERVER['HTTP_REFERER']}
<br />"; } else { print "You didn't click any links to get here<br />"; } ?> <a href="refer.php">Click me!</a>
If you load that page in your browser by typing the URL in by
hand, the "You didn't click any links to get here" text is shown because
HTTP_REFERER has not been set. However, if once the page is loaded you
follow the "Click me!" link, the page will reload itself; this time,
HTTP_REFERER will be set and the other message should appear. Although
it can be easily spoofed, HTTP_REFERER is generally a good way to make
sure a visitor came from a certain pagewhether you want to use that to say, "You
can't download my files because you came from another site" or "Welcome, Google
users!" is up to you.