Tuesday, October 20, 2009

Useful Server Variables








Useful Server Variables


There's a special superglobal array, $_SERVER, that contains a great deal of information about what's going on with your web application. For example, $_SERVER['PHP_SELF'] holds the name of the current script, $_SERVER['REQUEST_METHOD'] holds the request method that was used ("GET", "POST", and so on), $_SERVER['HTTP_USER_AGENT'] holds the type of the user's browser, and so on. You can see a sampling of the most useful server variables available in $_SERVER in Table 6-1.


Table 6-1. The General Server Variables

Server variable

Description

'AUTH_TYPE'

When running under Apache as module doing HTTP authentication, this variable holds the authentication type.

'DOCUMENT_ROOT'

The document root directory under which the script is executing, as defined in the server's configuration file.

'GATEWAY_INTERFACE'

The revision of the CGI specification that the server is using, such as 'CGI/1.1'.

'PATH_TRANSLATED'

File systembased path to the current script.

'PHP_AUTH_PW'

When running under Apache as module doing HTTP authentication, this variable holds the password provided by the user.

'PHP_AUTH_USER'

When running under Apache as module doing HTTP authentication, this variable holds the username provided by the user.

'PHP_SELF'

The filename of the currently executing script, relative to the document root.

'QUERY_STRING'

The query string, if any, with which the page was accessed.

'REMOTE_ADDR'

The IP address from which the user is viewing the current page.

'REMOTE_HOST'

The Host name from which the user is viewing the current page.

'REMOTE_PORT'

The port being used on the user's machine to communicate with the web server.

'REQUEST_METHOD'

Specifies which request method was used to access the page; such as 'GET', 'HEAD', 'POST', 'PUT'.

'REQUEST_URI'

The URI that was given in order to access this page, such as '/index.html'.

'SCRIPT_FILENAME'

The absolute pathname of the currently executing script.

'SCRIPT_NAME'

Contains the current script's path. This is useful for pages that need to point to themselves.

'SERVER_ADMIN'

The value given to the SERVER_ADMIN directive (for Apache) in the web server configuration file.

'SERVER_NAME'

The name of the server host under which the script is executing.

'SERVER_PORT'

The port on the server machine being used by the web server for communication. By default setups, this is '80'.

'SERVER_PROTOCOL'

Name and revision of the information protocol through which the page was requested; such as 'HTTP/1.0'.

'SERVER_SIGNATURE'

String containing the server version and virtual host name, which are added to server-generated pages.

'SERVER_SOFTWARE'

The server identification string.



NOTE



The $HTTP_SERVER_VARS array contains the same information as shown in Tables 6-1 and 6-2 but is not a superglobal.


Table 6-2. The HTTP Server Variables

HTTP variable

Description

'HTTP_ACCEPT'

Text in the Accept: header from the current request, if there is one.

'HTTP_ACCEPT_CHARSET'

Text in the Accept-Charset: header from the current request, if there is one, such as: '*, utf-8'.

'HTTP_ACCEPT_ENCODING'

Text in the Accept-Encoding: header from the current request, if there is one, such as: 'zip'.

'HTTP_ACCEPT_LANGUAGE'

Text in the Accept-Language: header from the current request, if there is one, such as 'en' for English.

'HTTP_CONNECTION'

Text in the Connection: header from the current request, if there is one, such as: 'Keep-Alive'.

'HTTP_HOST'

Text in the Host: header from the current request, if there is one.

'HTTP_REFERER'

The address of the page (if any) that referred the user agent to the current page. The browser sets this.

'HTTP_USER_AGENT'

Text in the User-Agent: header from the current request, if there is one. This is a string denoting the browser that is accessing the page.




These server variables are great because they give you a little more informationsuch as the name of the script that's running in this example, phpidentifier.php:



<HTML>
<HEAD><TITLE>The Self Identifier Script</TITLE></HEAD>
<BODY>
<H1>The Self Identifier Script</H1>
<?php
echo "Welcome to ", $_SERVER["PHP_SELF"];
?>
</BODY>
</HTML>


Here's what you see when you run this script:



Welcome to /phpindentifier.php








    No comments: