PHP Variable SERVER


$_SERVER is an array that contains information such as headers, paths, and script locations. It is a super global variable in PHP, and we can access it directly from anywhere in the PHP program.

$_SERVER contains a lot of information, you can try to print it directly:

print_r($_SERVER);

Limited to space, no longer print the results listed here, and different servers, print out the results are also different.


$_SERVER array element

In more cases, we only need some elements of the $_SERVER array(array elements). The $_SERVER array contains the following elements:


Page Program Related

  • $_SERVER['PHP_SELF']: Path relative to the root of the site and The name of the PHP program, relative to the document root.
  • $_SERVER['HTTP_REFERER']: Link to the previous page of the current page Of the URL address.
  • $_SERVER['SCRIPT_NAME']: Path relative to the root of the site and PHP program file name.
  • $_SERVER['REQUEST_URI']: The URI required to access this page.
  • $_SERVER['SCRIPT_FILENAME']: The absolute path of the currently running PHP program and file name.
  • $_SERVER['PATH_TRANSLATED']: The current path to the file system where the PHP program is located(not the root of the document).
  • $_SERVER['QUERY_string']: The query string(the first question mark in the URL after the content but does not include the content behind #).
  • $_SERVER['argv']: Parameters passed to the current PHP program.
  • $_SERVER['argc']: In command line mode, contains the number of command-line arguments passed to the program. $_SERVER['REQUEST_TIME']: The timestamp at the beginning of the request, as of PHP 5.1.0. $_SERVER['REQUEST_METHOD']: The request method when accessing the page, eg "GET", "HEAD", "POST" or "PUT".
  • $_SERVER['HTTP_ACCEPT']: The content of the Accept: header of the current request.
  • $_SERVER['HTTP_ACCEPT_CHARSET']: The content of the Accept-Charset: header of the current request. For example: "iso-8859-1, *, utf-8".
  • $_SERVER['HTTP_ACCEPT_ENCODING']: The contents of the Accept-Encoding: header of the current request. For example: "gzip".
  • $_SERVER['HTTP_ACCEPT_LANGUAGE']: The content of the Accept-Language: header of the current request. For example: "zh-cn".
  • $_SERVER['HTTP_CONNECTION']: The contents of the currently requested Connection: header. For example: "Keep-Alive".
  • $_SERVER['HTTP_HOST']: The contents of the Host: header of the current request.
  • $_SERVER['HTTPS']: If the PHP program is accessed via the HTTPS protocol, it is set to a non-null value.
  • $_SERVER['PHP_AUTH_DIGEST']: This variable is set to the "Authorization" HTTP header sent by the client(for further authentication) during HTTP Digest authentication when running as an Apache module.
  • $_SERVER['PHP_AUTH_USER']: When PHP is running under Apache or IIS(PHP 5 ISAPI) module mode and is using HTTP authentication, this variable is the user name entered by the user.
  • $_SERVER['PHP_AUTH_PW']: When PHP is running under Apache or IIS(PHP 5 ISAPI) module mode and is using HTTP authentication, this variable is the password entered by the user.
  • $_SERVER['AUTH_TYPE']: When PHP is running in Apache module mode and is using HTTP authentication, this variable is the type of authentication.

Server Side Related

  • $_SERVER['DOCUMENT_ROOT']: The root directory of the document where the PHP program is currently running, as defined in the server configuration file.
  • $_SERVER['GATEWAY_INTERFACE']: Version of the CGI specification used by the server, eg "CGI /1.1".
  • $_SERVER['SERVER_ADDR']: The IP address of the server where the PHP program is currently running.
  • $_SERVER['SERVER_NAME']: The name of the server where the PHP program is currently running.
  • $_SERVER['SERVER_ADMIN']: SERVER_ADMIN parameter in the Apache server configuration file.
  • $_SERVER['SERVER_PORT']: The port used by the server. If SSL is used, this value is the HTTP port set by the user.
  • $_SERVER['SERVER_SIGNATURE']: A string containing the server version and the virtual host name.
  • $_SERVER['SERVER_SOFTWARE']: A string of server identifiers, given in the header information in response to the request.
  • $_SERVER['SERVER_PROTOCOL']: Name and version of the communication protocol when requesting the page, eg "HTTP /1.0".

Other Miscellaneous

  • $_SERVER['HTTP_USER_AGENT']: The content of the currently requested User-Agent: header, which indicates the user agent accessing the page.
  • $_SERVER['REMOTE_ADDR']: The IP address of the user who is viewing the current page.
  • $_SERVER['REMOTE_HOST']: The name of the user who is browsing the current page.
  • $_SERVER['REMOTE_PORT']: The port the user is using to connect to the server.

Note that if you run PHP from the command line, the elements listed above are almost nonexistent(or of no practical significance).




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook