Home > Article > Backend Development > PHP predefined variables
Predefined variables
Server variable: $_SERVER
Note: Used in PHP (as the current mainstream development language) 4.1.0 and later versions. Previous versions, used $HTTP_SERVER_VARS.
$_SERVER is an array containing things like headers, paths, and script locations. The entities of the array are created by the web server. There is no guarantee that all servers will produce all messages; servers may ignore some messages or generate new messages not listed below. This means that a lot of these variables are specified in the cgi (which not many people use anymore) 1.1 specification, so you should study it carefully.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts. You do not need to use global $_SERVER; to access it within a function or method, as you would with $HTTP_SERVER_VARS.
$HTTP_SERVER_VARS contains the same information, but is not an automatic global variable. (Note: $HTTP_SERVER_VARS and $_SERVER are different variables, and PHP (as the current mainstream development language) handles them differently.)
If the register_globals directive is set, these variables are also available in all scripts; that is, Separated $_SERVER and $HTTP_SERVER_VARS arrays. For related information, see the security-related chapter Using Register Globals. These individual global variables are not automatic global variables.
You may find that some of the $_SERVER elements listed below are not available. Note that if you run PHP from the command line (as the current mainstream development language), almost none of the elements listed below are valid (or have no practical meaning).
"php (as the current mainstream development language)_SELF"
The file name of the currently executing script, related to the document root. For example, use $_SERVER[php(as the current mainstream development language)_SELF in the script with the URL address http://example.com/test.php(as the current mainstream development language)/foo.bar ] will get the result of /test.php (as the current mainstream development language)/foo.bar.
If php (as the current mainstream development language) is run in command line mode, this variable is invalid.
"argv"
The arguments passed to this script. When the script is run in command-line mode, the argv variable is passed to the program as C-style command-line arguments. When the GET method is called, this variable contains the requested data.
"argc"
Contains the number of command line arguments passed to the program (if running in command line mode).
"GATEWAY_INTERFACE"
The standard version of cgi used by the server (not many people are using it now). For example, "cgi (not many people use it now)/1.1".
SERVER_NAME
The name of the server host where the script is currently running. If the script is running on a virtual host, the name is determined by the value set for that virtual host.
SERVER_SOFTWARE
The string identifying the server, given in the header when responding to the request.
"SERVER_PROTOCOL"
The name and version of the communication protocol when requesting the page. For example, "HTTP/1.0".
"REQUEST_METHOD"
The request method when accessing the page. For example: "GET", "HEAD", "POST", "PUT".
"QUERY_STRING"
The string of query.
"DOCUMENT_ROOT"
The document root directory where the currently running script is located. Defined in the server configuration file.
"HTTP_ACCEPT"