Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

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

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.

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page