The variables included in PHP $ _ SERVER are global variables, which can be called Super global variables. Below I will give you a detailed explanation of common PHP $ _ SERVER usage, hoping to help you.
Frequently used $ _ SERVER [] Variables
$ _ SERVER ['HTTP _ ACCEPT_LANGUAGE '] // browser Language
$ _ SERVER ['remote _ ADDR '] // IP address of the current user.
$ _ SERVER ['remote _ host'] // HOST name of the current user
$ _ SERVER ['request _ URI '] // URL
$ _ SERVER ['remote _ port'] // PORT.
$ _ SERVER ['server _ name'] // NAME of the SERVER host.
$ _ SERVER ['php _ SELF '] // File Name of the script being executed
$ _ SERVER ['argv'] // parameters passed to the script.
$ _ SERVER ['argc '] // Number of command line parameters passed to the program.
$ _ SERVER ['Gateway _ interface'] // CGI standard version.
$ _ SERVER ['server _ soft'] // string of the SERVER ID
$ _ SERVER ['server _ Protocol'] // name and version of the communication PROTOCOL on the request page
$ _ SERVER ['request _ method'] // request method for accessing the page
$ _ SERVER ['query _ string'] // query string.
$ _ SERVER ['document _ root'] // ROOT directory of the DOCUMENT where the script is currently running
$ _ SERVER ['HTTP _ ACCEPT '] // Accept of the current request: Content in the header.
$ _ SERVER ['HTTP _ ACCEPT_CHARSET '] // The Accept-Charset of the current request: Content in the header.
$ _ SERVER ['HTTP _ ACCEPT_ENCODING '] // The Accept-Encoding of the current request: Content in the header
$ _ SERVER ['HTTP _ connection'] // CONNECTION of the current request: Content in the header. For example, "Keep-Alive ".
$ _ SERVER ['HTTP _ host'] // HOST of the current request: Content in the header.
$ _ SERVER ['HTTP _ referer'] // URL of the previous page linked to the current page.
$ _ SERVER ['HTTP _ USER_AGENT '] // User_Agent of the current request: Content in the header.
$ _ SERVER ['https'] // If accessed through HTTPS, it is set to a non-null value (on); otherwise, off is returned.
$ _ SERVER ['script _ filename'] # the absolute path name of the currently executed SCRIPT.
$ _ SERVER ['server _ admin'] # administrator Information
$ _ SERVER ['server _ port'] # PORT used by the SERVER
$ _ SERVER ['server _ SIGNATURE '] # a string containing the SERVER version and virtual host name.
$ _ SERVER ['path _ TRANSLATED '] # basic PATH of the file system where the current script is located (not the root directory of the document.
$ _ SERVER ['script _ name'] # contains the path of the current SCRIPT. This is useful when the page needs to point to itself.
$ _ SERVER ['php _ AUTH_USER '] # when PHP runs in the Apache module mode and uses the HTTP authentication function, this variable is the user name entered by the user.
$ _ SERVER ['php _ AUTH_PW '] # when PHP runs in the Apache module mode and is using the HTTP authentication function, this variable is the password entered by the user.
$ _ SERVER ['auth _ type'] # when PHP runs in the Apache module mode and is using the HTTP authentication function, this variable is the authentication TYPE
Page program Problems
• $ _ SERVER ['php _ SELF ']: relative to the path of the website root directory and the PHP program name, it is related to document root.
• $ _ SERVER ['HTTP _ referer']: the URL of the previous page linked to the current page.
• $ _ SERVER ['script _ name']: the path relative to the root directory of the website and the NAME of the PHP program file.
• $ _ SERVER ['request _ URI ']: URI required to access this page.
• $ _ SERVER ['script _ filename']: the absolute path and file name of the currently running PHP program.
• $ _ SERVER ['path _ TRANSLATED ']: The basic PATH of the file system (not the document root directory) where the current PHP program is located.
• $ _ SERVER ['query _ string']: query string (the first question mark in the URL? After the content, but does not include # After the content ).
• $ _ SERVER ['argv']: The parameter passed to the current PHP program.
• $ _ SERVER ['argc ']: Number of command line parameters passed to the program in command line mode.
• $ _ SERVER ['request _ time']: The TIME stamp at the start of the REQUEST, which is valid from PHP 5.1.0.
• $ _ SERVER ['request _ method']: The request method used to access the page, for example, "GET", "HEAD", "POST", or "PUT ".
• $ _ SERVER ['HTTP _ ACCEPT ']: The Accept of the current request: the content of the header information.
• $ _ SERVER ['HTTP _ ACCEPT_CHARSET ']: The Accept-Charset of the current request: the content of the header information. Example: "iso-8859-1, *, UTF-8 ".
• $ _ SERVER ['HTTP _ ACCEPT_ENCODING ']: The Accept-Encoding of the current request: the content of the header information. For example, "gzip ".
• $ _ SERVER ['HTTP _ ACCEPT_LANGUAGE ']: The Accept-Language of the current request: the content of the header information. For example, "zh-cn ".
• $ _ SERVER ['HTTP _ connection']: the CONNECTION of the current request: the content of the header information. For example, "Keep-Alive ".
• $ _ SERVER ['HTTP _ host']: the HOST of the current request: the content of the header information.
• $ _ SERVER ['https']: If the PHP program is accessed through the HTTPS protocol, it is set to a non-empty value.
• $ _ SERVER ['php _ AUTH_DIGEST ']: during HTTP Digest authentication when running as an Apache module, this variable is set to the "Authorization" HTTP header content sent by the client (For further authentication ).
• $ _ SERVER ['php _ AUTH_USER ']: when PHP runs in the Apache or IIS (PHP 5 is an ISAPI) module and is using the HTTP authentication function, this variable is the user name entered by the user.
• $ _ SERVER ['php _ AUTH_PW ']: when PHP runs in the Apache or IIS (PHP 5 is an ISAPI) module and is using the HTTP authentication function, this variable is the password entered by the user.
• $ _ SERVER ['auth _ type']: when PHP runs in the Apache module mode and is using the HTTP authentication function, this variable is the authentication TYPE.
Server-related
• $ _ SERVER ['document _ root']: the ROOT directory of the DOCUMENT where the PHP program is currently running, which is defined in the SERVER configuration file.
• $ _ SERVER ['Gateway _ interface']: The CGI standard version used by the SERVER, for example, "CGI/1.1 ".
• $ _ SERVER ['server _ ADDR ']: IP address of the SERVER on which the PHP program is currently running.
• $ _ SERVER ['server _ name']: NAME of the SERVER on which the PHP program is currently running.
• $ _ SERVER ['server _ admin']: SERVER_ADMIN parameter in the Apache SERVER configuration file.
• $ _ SERVER ['server _ port']: PORT used by the SERVER. If SSL secure connection is used, this value is the HTTP port set by the user.
• $ _ SERVER ['server _ SIGNATURE ']: a string containing the SERVER version and virtual host name.
• $ _ SERVER ['server _ soft']: the string of the SERVER identifier, which is provided in the header of the Response Request.
• $ _ SERVER ['server _ Protocol']: Name and version of the communication PROTOCOL used to request the page, for example, "HTTP/1.0 ".
Miscellaneous
• $ _ SERVER ['HTTP _ USER_AGENT ']: The content of the User-Agent: header of the current request. This string indicates the information of the User Agent accessing the page.
• $ _ SERVER ['remote _ ADDR ']: the IP address of the user on the current page is being viewed.
• $ _ SERVER ['remote _ host']: The HOST Name of the user browsing the current page.
• $ _ SERVER ['remote _ port']: The PORT used by the user to connect to the SERVER.
Note that if you run PHP In the command line mode, the elements listed above are hardly valid (or have no practical significance ).
Example 1
PHP retrieves the current url path
1, $ _ SERVER ["QUERY_STRING"]
Description: query string
2, $ _ SERVER ["REQUEST_URI"]
Description: The URI required to access this page
3, $ _ SERVER ["SCRIPT_NAME"]
Description: path containing the current script
4, $ _ SERVER ["PHP_SELF"]
Description: File Name of the script being executed
Instance:
1, http://www.bKjia. c0m/(open the home page directly)
Result:
$ _ SERVER ["QUERY_STRING"] = ""
$ _ SERVER ["REQUEST_URI"] = "/"
$ _ SERVER ["SCRIPT_NAME"] = "/index. php"
$ _ SERVER ["PHP_SELF"] = "/index. php"
Http://www.bKjia. c0m /? P = 222 (with query)
Result:
$ _ SERVER ["QUERY_STRING"] = "p = 222"
$ _ SERVER ["REQUEST_URI"] = "/? P = 222"
$ _ SERVER ["SCRIPT_NAME"] = "/index. php"
$ _ SERVER ["PHP_SELF"] = "/index. php"
Http://www.bKjia. c0m/index. php? P = 222 & q = biuuu
Result:
$ _ SERVER ["QUERY_STRING"] = "p = 222 & q = biuuu"
$ _ SERVER ["REQUEST_URI"] = "/index. php? P = 222 & q = biuuu"
$ _ SERVER ["SCRIPT_NAME"] = "/index. php"
$ _ SERVER ["PHP_SELF"] = "/index. php"
$ _ SERVER ["QUERY_STRING"]: Obtain the query statement. Which of the following statements can be obtained in the instance? Value after
$ _ SERVER ["REQUEST_URI"] gets the value after http://www.bKjia. c0m, including/
$ _ SERVER ["SCRIPT_NAME"] obtains the path of the current script, for example, index. php.
$ _ SERVER ["PHP_SELF"] File Name of the script currently being executed
Current url: "http: //". $ _ SERVER ['HTTP _ host']. $ _ SERVER ['php _ SELF ']
To sum up, for QUERY_STRING, REQUEST_URI, SCRIPT_NAME, and PHP_SELF, an in-depth understanding will help us call these four values correctly in the $ _ SERVER function. Explain the differences between the four variables in the $ _ SERVER function: QUERY_STRING, REQUEST_URI, SCRIPT_NAME, and PHP_SELF.