Home >Backend Development >PHP Tutorial >Detailed explanation of how to use PHP $_SERVER variable_PHP tutorial
The variables included in PHP $_SERVER are all global variables and can be called super global variables. Below I will sort out the commonly used PHP $_SERVER usage details. I hope it will be helpful to everyone.
My commonly used $_SERVER[] variable
$_SERVER['HTTP_ACCEPT_LANGUAGE']//Browser language
$_SERVER['REMOTE_ADDR'] //Current user IP.
$_SERVER['REMOTE_HOST'] //Current user host name
$_SERVER['REQUEST_URI'] //URL
$_SERVER['REMOTE_PORT'] //Port.
$_SERVER['SERVER_NAME'] //The name of the server host.
$_SERVER['PHP_SELF']//The file name of the script being executed
$_SERVER['argv'] //The parameters passed to the script.
$_SERVER['argc'] //The number of command line parameters passed to the program.
$_SERVER['GATEWAY_INTERFACE']//The version of the CGI specification.
$_SERVER['SERVER_SOFTWARE'] //String of server identification
$_SERVER['SERVER_PROTOCOL'] //The name and version of the communication protocol when requesting the page
$_SERVER['REQUEST_METHOD']//Request method when accessing the page
$_SERVER['QUERY_STRING'] //Query string.
$_SERVER['DOCUMENT_ROOT'] //The document root directory where the currently running script is located
$_SERVER['HTTP_ACCEPT'] //Contents of the Accept: header of the current request.
$_SERVER['HTTP_ACCEPT_CHARSET'] //Contents of the Accept-Charset: header of the current request.
$_SERVER['HTTP_ACCEPT_ENCODING'] //Contents of the Accept-Encoding: header of the current request
$_SERVER['HTTP_CONNECTION'] //Contents of the Connection: header of the current request. For example: "Keep-Alive".
$_SERVER['HTTP_HOST'] //Contents of the Host: header of the current request.
$_SERVER['HTTP_REFERER'] //The URL address of the previous page linked to the current page.
$_SERVER['HTTP_USER_AGENT'] //Contents of the User_Agent: header of the current request.
$_SERVER['HTTPS']//If accessed through https, it is set to a non-empty value (on), otherwise it returns off
$_SERVER['SCRIPT_FILENAME'] #The absolute path name of the currently executing script.
$_SERVER['SERVER_ADMIN'] #Administrator information
$_SERVER['SERVER_PORT'] #The port used by the server
$_SERVER['SERVER_SIGNATURE'] #A string containing the server version and virtual host name.
$_SERVER['PATH_TRANSLATED'] #The base path of the file system (not the document root directory) where the current script is located.
$_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 is running in Apache module mode and is using the HTTP authentication function, this variable is the username entered by the user.
$_SERVER['PHP_AUTH_PW'] #When PHP is running in Apache module mode and is using the HTTP authentication function, this variable is the password entered by the user.
$_SERVER['AUTH_TYPE'] #When PHP is running in Apache module mode and is using the HTTP authentication function, this variable is the authentication type
Page program related
•$_SERVER['PHP_SELF']: The path relative to the website root directory and the PHP program name, related to the document root.
•$_SERVER['HTTP_REFERER']: URL address 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']: The 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 base path of the file system (not the document root directory) where the current PHP program is located.
•$_SERVER['QUERY_STRING']: Query string (the content after the first question mark ? in the URL but excluding the content after #).
•$_SERVER['argv']: Arguments passed to the current PHP program.
•$_SERVER['argc']: In command line mode, contains the number of command line parameters passed to the program.
•$_SERVER['REQUEST_TIME']: timestamp when the request started, valid as of PHP 5.1.0.
•$_SERVER['REQUEST_METHOD']: The request method when accessing the page, such as: "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 content 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 content of the Connection: header information of the current request. For example: "Keep-Alive".
•$_SERVER['HTTP_HOST']: The content of the Host: header information of the current request.
•$_SERVER['HTTPS']: Set to a non-empty value if the PHP program is accessed through the HTTPS protocol.
•$_SERVER['PHP_AUTH_DIGEST']: When running as an Apache module, during the HTTP Digest authentication process, this variable is set to the "Authorization" HTTP header content sent by the client (for further authentication operations).
•$_SERVER['PHP_AUTH_USER']: When PHP is running in Apache or IIS (PHP 5 is ISAPI) module mode, and the HTTP authentication function is being used, this variable is the username entered by the user.
•$_SERVER['PHP_AUTH_PW']: When PHP is running in Apache or IIS (PHP 5 is ISAPI) module mode, and the HTTP authentication function is being used, this variable is the password entered by the user.
•$_SERVER['AUTH_TYPE']: When PHP is running in Apache module mode and the HTTP authentication function is being used, this variable is the authentication type.
Server-side related
•$_SERVER['DOCUMENT_ROOT']: The document root directory where the PHP program is currently running, defined in the server configuration file.
•$_SERVER['GATEWAY_INTERFACE']: The version of the CGI specification used by the server, for example: "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 using SSL secure connection, 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_SOFTWARE']: The string identifying the server, given in the header information when responding to the request.
•$_SERVER['SERVER_PROTOCOL']: The name and version of the communication protocol when requesting the page, for example: "HTTP/1.0".
Other miscellaneous items
•$_SERVER['HTTP_USER_AGENT']: The content of the User-Agent: header information 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 who is browsing the current page.
•$_SERVER['REMOTE_HOST']: The host name of the user who is browsing the current page.
•$_SERVER['REMOTE_PORT']: The port used by users to connect to the server.
Note that few of the elements listed above are valid (or have no real meaning) if you run PHP from the command line.
Example 1
PHP gets the current url path
1,$_SERVER["QUERY_STRING"]
Description: Query string
2.$_SERVER["REQUEST_URI"]
Description: URI required to access this page
3,$_SERVER["SCRIPT_NAME"]
Description: Contains the path of the current script
4,$_SERVER["PHP_SELF"]
Description: The file name of the currently executing script
Example:
1. http://www.bKjia.c0m/ (Open the homepage directly)
Result:
$_SERVER["QUERY_STRING"] = ""
$_SERVER["REQUEST_URI"] = "/"
$_SERVER["SCRIPT_NAME"] = "/index.php"
$_SERVER["PHP_SELF"] = "/index.php"
2, 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"
3, 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"] obtains the query statement. As can be seen from the example, what is obtained is the value after ?
$_SERVER["REQUEST_URI"] Get the value after http://www.bKjia.c0m, including /
$_SERVER["SCRIPT_NAME"] gets the path of the current script, such as: index.php
$_SERVER["PHP_SELF"] The file name of the currently executing script
Current url: "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']
To summarize, for QUERY_STRING, REQUEST_URI, SCRIPT_NAME and PHP_SELF, an in-depth understanding will help us correctly call these four values in the $_SERVER function. Learn the differences between the four variables QUERY_STRING, REQUEST_URI, SCRIPT_NAME and PHP_SELF in the $_SERVER function through detailed examples.