$ _SERVER server variables in PHP

Lecture



Note: First appeared in PHP version 4.1.0. Earlier versions use the $ HTTP_SERVER_VARS variable.

$ _SERVER is a superglobal array containing information such as headers, paths, script location. This array is created by the web server. There are no guarantees that the web server will provide this superglobal array with all the predefined information. For detailed information on the variables contained in this superglobal array, refer to the CGI 1.1 specification .

Superglobals are available inside user functions. That is, you do not need to declare superglobal variables global inside user functions, doing something like this: global $_SERVER . This is not affected even by the PHP configuration settings (php.ini).

The $ HTTP_SERVER_VARS variable contains the same initial information, but it is not an autoglobal variable. (Note that $ HTTP_SERVER_VARS and $ _SERVER are different variables).

You may not find some variables that may be contained in the $ _SERVER array if you run PHP from the command line.

PHP_SELF

The file name of the currently running script, relative to the root of the document. For example, $ _SERVER ['PHP_SELF'] in the script at the address http://example.com/test.php/foo.bar returns /test.php/foo.bar . The predefined constant __FILE__ contains the full path and file name.
If PHP is run from the command line, this variable contains the name of the script (available starting with PHP 4.3.0).

argv

Array of parameters passed to the script. When using the GET method, it will contain the query string.

argc

Contains the number of command line parameters passed to the script (if passed from the command line).

GATEWAY_INTERFACE

Contains the version of CGI that the web server uses (for example, CGI / 1.1).

SERVER_NAME

The name of the web server under which the current script is executed. If the script is executed on a virtual web server, the variable will contain the name of the web server that was defined for the virtual web server.

SERVER_SOFTWARE

Identification string of the web server, which is transmitted through the headers when answering server requests.

SERVER_PROTOCOL

The name and version of the HTTP protocol used (for example, HTTP / 1.1).

REQUEST_METHOD

Web page request method used ('GET', 'HEAD', 'POST', 'PUT').

REQUEST_TIME

The start time of the webpage request. Available starting with PHP 5.1.0

QUERY_STRING

The query string of the web page, if it exists, through which the page was accessed.

DOCUMENT_ROOT

The root directory under which the current script runs. Determined by the web server configuration.

HTTP_ACCEPT

The contents of the Accept header, if any.

HTTP_ACCEPT_CHARSET

The contents of the Accept-Charset header, if any. Example: ' iso-8859-1, *, utf-8 '.

HTTP_ACCEPT_ENCODING

The contents of the Accept-Encoding header, if any. Example: ' gzip '.

HTTP_ACCEPT_LANGUAGE

The contents of the Accept-Language header, if any. Example: ' en '.

HTTP_CONNECTION

The content of the Connection header, if any. Example: ' Keep-Alive '.

HTTP_HOST

The contents of the Host header, if any.

HTTP_REFERER

The address of the page that links to the current document through a custom client (client software). Not all user clients provide this information, and some of them are able to change HTTP_REFERER, therefore, such information cannot be fully trusted.

HTTP_USER_AGENT

The contents of the header 'User-Agent'. This line contains information about the user client (client software) that accesses the page. Typical example: Mozilla / 4.5 [en] (X11; U; Linux 2.2.9 i586) . You can also use the get_browser () function to get this information.

REMOTE_ADDR

The IP address of the remote user who accesses the current page.

REMOTE_HOST

The host name of the remote user who accesses the current page. DNS reverse lookup is based on the remote user’s REMOTE_ADDR . Note: the web server must support the creation of this variable. For example, in Apache, the HostnameLookups option should be enabled in the httpd.conf ( On ) configuration file. See also: gethostbyaddr ()

REMOTE_PORT

Port used to connect to the web server

SCRIPT_FILENAME

The absolute path for the current script.

SERVER_ADMIN

The value specified by SERVER_ADMIN (for Apache). Determined by the web server configuration directive. If the script is executed on a virtual host, the value is determined by the current settings of the virtual host of the web server.

SERVER_PORT

The current port of the web server used for HTTP data access. The default is '80'.

SERVER_SINGATURE

A string containing the version of the web server or the name of the virtual host.

PATH_TRANSLATED

The base path to the current script.

SCRIPT_NAME

Contains the path and file name of the current executable script.

REQUEST_URI

The URI for the current page, for example, ' /index.html '.

PHP_AUTH_DIGEST

If PHP works as an Apache module, the variable is used in HTTP authentication, for authentication.

PHP_AUTH_USER

If PHP works as an Apache module, or as ISAPI IIS, this variable contains the name of the user being authenticated using HTTP authentication.

PHP_AUTH_PW

If PHP works as an Apache module, or as ISAPI IIS, this variable contains the password of the user being authenticated using HTTP authentication.

AUTH_TYPE

If PHP works as an Apache module, or as ISAPI IIS, this variable contains the type of authentication used by the HTTP protocol.

$ _ENV environment variables

Available starting with PHP version 4.1.0. In earlier versions of PHP, use $ HTTP_ENV_VARS.

These variables are imported into the global PHP namespace from the environment in which the PHP interpreter is running. The number and types of variables depend on the shell in which PHP works, so the list of these variables cannot be given. See the documentation for the shell you are using for a list of your environment variables.

Some environment variables are contained in CGI variables if you use PHP as CGI.

The $ _ENV environment variables are available inside user-defined functions. That is, you do not need to declare superglobals as global inside user functions, doing something like this: global $_ENV . This is not affected even by the PHP configuration settings (php.ini).

The $ HTTP_ENV_VARS variable contains the same initial information, but it is not an autoglobal variable. (Note that $ HTTP_ENV_VARS and $ _ENV are different variables).

HTTP Cookies: $ _COOKIE

Available starting with PHP version 4.1.0. In earlier versions of PHP, use $ HTTP_COOKIE_VARS

An associative array of variables passed to the current script via HTTP Cookies.

HTTP cookie variables are available within user functions. That is, you do not need to declare superglobals as global inside user-defined functions, doing something like this: global $_COOKIE . This is not affected even by the PHP configuration settings (php.ini).

The $ HTTP_COOKIE_VARS variable contains the same initial information, but it is not an autoglobal variable. (Note that $ HTTP_COOKIE_VARS and $ _COOKIE are different variables.)

HTTP GET variables: $ _GET

Available starting with PHP version 4.1.0. In earlier versions of PHP, use $ HTTP_GET_VARS.

An associative array of variables passed to the current script via HTTP GET requests.

HTTP GET variables are available within user-defined functions. That is, you do not need to declare superglobal variables to be global inside user-defined functions, doing something like this: global $_GET . This is not affected even by the PHP configuration settings (php.ini).

The $ HTTP_GET_VARS variable contains the same initial information, but it is not an autoglobal variable. (Note that $ HTTP_GET_VARS and $ _GET are different variables).

HTTP POST variables: $ _POST

Available starting with PHP version 4.1.0. In earlier versions of PHP, use $ HTTP_POST_VARS.

An associative array of variables passed to the current script via HTTP POST requests.

HTTP POST variables are available within user-defined functions. That is, you do not need to declare superglobal variables to be global inside user-defined functions, doing something like this: global $_POST . This is not affected even by the PHP configuration settings (php.ini).

The $ HTTP_POST_VARS variable contains the same initial information, but it is not an autoglobal variable. (Note that $ HTTP_POST_VARS and $ _POST are different variables.)

File Uploads HTTP Variables: $ _FILES

Available starting with PHP version 4.1.0. In earlier versions of PHP, use $ HTTP_POST_FILES.

An associative array of variables passed to the current script via HTTP POST requests.

HTTP POST variables are available within user-defined functions. That is, you do not need to declare superglobal variables global inside user-defined functions, doing something like this: global $_FILES . This is not affected even by the PHP configuration settings (php.ini).

The $ HTTP_POST_FILES variable contains the same initial information, but it is not an autoglobal variable. (Note that $ HTTP_POST_FILES and $ _FILES are different variables.)

Query variables: $ _REQUEST

Available starting with PHP version 4.1.0. In earlier versions of PHP has no equivalents. Starting with PHP version 4.3.0 $ _REQUEST also includes $ _FILES .

An associative array containing $ _GET , $ _POST , and $ _COOKIE

The query variables $ _REQUEST are available inside user-defined functions. That is, you do not need to declare superglobal variables global inside user-defined functions, doing something like this: global $_REQUEST . This is not affected even by the PHP configuration settings (php.ini).

Session variables $ _SESSION

Available starting with PHP version 4.1.0. In earlier versions of PHP, use $ HTTP_SESSION_VARS.

An associative array containing session variables available for the current script. For more information, see session functions.

Session variables are available inside user-defined functions. That is, you do not need to declare superglobal variables to be global inside user-defined functions, doing something like this: global $_SESSION . This is not affected even by the PHP configuration settings (php.ini).

The variable $ HTTP_SESSION_VARS contains the same initial information, but it is not an autoglobal variable. (Note that $ HTTP_SESSION_VARS and $ _SESSION are different variables.)

Global Variables: $ GLOBALS

Global variables are available starting with PHP3.

An associative array containing references to all variables that are currently defined in the global script area. Variable names are array keys.

Global variables are available inside user-defined functions. That is, you do not need to declare superglobal variables global inside user-defined functions, doing something like this: global $GLOBALS . This is not affected even by the PHP configuration settings (php.ini).

Last PHP script error message: $ php_errormsg

The variable $ php_errormsg contains the last PHP script error message.

This variable will be available only within the area in which the error occurred, and only if the configuration option track_errors is enabled (it is disabled by default).


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Running server side scripts using PHP as an example (LAMP)

Terms: Running server side scripts using PHP as an example (LAMP)