Php pre-defined variable help (with instance)

Source: Internet
Author: User
Tags http authentication http digest authentication http file upload
The preferred method for php to obtain external variables is to use the hyperglobal variables mentioned below. Prior to this, people were either dependent on register_globals or long pre-defined PHP array ($ HTTP _ * _ VARS ). Starting from PHP5.0.0 and long format PHP pre-defined variables can be set from php 4.1.0. the preferred way to get external variables is to use the Superglobal variables mentioned below. Prior to this, people were either dependent on register_globals or long pre-defined PHP array ($ HTTP _ * _ VARS ). From PHP 5.0.0, you can set register_long_arrays to block long-format PHP predefined variables.
SERVER variable: $ _ SERVER
Note: PHP 4.1.0 and later versions are used. In earlier versions, $ HTTP_SERVER_VARS is used.
$ _ SERVER is an array containing header information (header), path, and script locations. The object of the array is created by the web server. It is not guaranteed that all servers can generate all information. the server may ignore some information or generate new information not listed below. This means that a large number of these variables are described in the CGI 1.1 specification, so we should take a closer look.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ _ SERVER; to access a function or method, just like using $ HTTP_SERVER_VARS.
$ HTTP_SERVER_VARS contains the same information, but it is not an automatic global variable (note: $ HTTP_SERVER_VARS and $ _ SERVER are different variables, and PHP processes them differently ).
If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ SERVER and $ HTTP_SERVER_VARS arrays are separated. For more information, see the security section using Register Globals. These independent global variables are not automatic global variables.
Some $ _ SERVER elements listed below may be unavailable. Note: If you run PHP in the command line mode, the elements listed below are hardly valid (or have no practical significance ).
"PHP_SELF"
The file name of the script being executed, which is related to document root. For example, using $ _ SERVER ['php _ SELF '] in a script with a URL address of http://example.com/test.php/foo.bar will get the result of/test. PHP/foo. bar. The _ FILE _ constant contains the absolute path and FILE name of the current (such as include) FILE.
If PHP runs as a command line, this variable is invalid before PHP 4.3.0.
"Argv"
Parameters passed to the script. When the script runs in the command line mode, the argv variable is passed to the command line parameters in the C language style of the program. When the GET method is called, the variable contains the requested data.
"Argc"
Contains the number of command line parameters passed to the program (if it is in command line mode ).
"GATEWAY_INTERFACE"
The CGI specification version used by the server. For example, "CGI/1.1 ".
"SERVER_NAME"
Name of the server host where the script is currently running. If the script runs on a VM, the name is determined by the value set by that VM.
"SERVER_SOFTWARE"
The string identified by the server, which is provided in the header information in the response request.
"SERVER_PROTOCOL"
The name and version of the communication protocol on the request page. For example, "HTTP/1.0 ".
"REQUEST_METHOD"
The request method used to access the page. For example, "GET", "HEAD", "POST", and "PUT ".
Note: If the request method is HEAD, the PHP script will stop sending the header information (this means no output buffer is available after any output is generated ).
"REQUEST_TIME"
The start time of the request. Valid from PHP 5.1.0.
"QUERY_STRING"
Query string (the first question mark in the URL? ).
"DOCUMENT_ROOT"
The document root directory where the script is currently running. Defined in the server configuration file.
"HTTP_ACCEPT"
The Accept of the current request: the content of the header information.
"HTTP_ACCEPT_CHARSET"
The Accept-Charset of the current request: the content of the header information. Example: "ISO-8859-1, *, UTF-8 ".
"HTTP_ACCEPT_ENCODING"
The Accept-Encoding of the current request: the content of the header information. For example, "gzip ".
"HTTP_ACCEPT_LANGUAGE"
The Accept-Language of the current request: the content of the header information. For example, "en ".
"HTTP_CONNECTION"
Connection of the current request: header information. For example, "Keep-Alive ".
"HTTP_HOST"
Host of the current request: header information.
"HTTP_REFERER"
The URL of the previous page that is linked to the current page. Not all user proxies (browsers) will set this variable, and some can also manually modify HTTP_REFERER. Therefore, this variable is not always true and correct.
"HTTP_USER_AGENT"
User-Agent of the current request: header information. This string indicates the information of the user agent accessing the page. A typical example is Mozilla/4.5 [en] (X11; U; linux 2.2.9 i586 ). You can also use get_browser () to obtain this information.
"HTTPS"
If the script is accessed through the HTTPS protocol, it is set to a non-null value.
"REMOTE_ADDR"
Browsing the IP address of the user on the current page.
"REMOTE_HOST"
The host name of the user browsing the current page. Reverse domain name resolution is based on the user's REMOTE_ADDR.
Note: You must configure the Web server to create this variable. For example, Apache needs to include HostnameLookups On in httpd. conf. See gethostbyaddr ().
"REMOTE_PORT"
The port used by the user to connect to the server.
"SCRIPT_FILENAME"
The absolute path name of the currently executed script.
Note: If the script is executed in CLI, it is used as a relative path, such as file. php or .. /file. php, $ _ SERVER ['script _ filename'] will contain the relative path specified by the user.

"SERVER_ADMIN"
This value specifies the SERVER_ADMIN parameter in the Apache server configuration file. If the script runs on a VM, this value is the value of that VM.
"SERVER_PORT"
The port used by the server. The default value is "80 ". If SSL secure connection is used, this value is the HTTP port set by the user.
"SERVER_SIGNATURE"
A string containing the server version and virtual host name.
"PATH_TRANSLATED"
The basic path of the file system (not the document root directory) where the current script is located. This is the result of a virtual image to a real path on the server.
Note: After PHP 4.3.2, PATH_TRANSLATED does not implicitly assign values like Apache 1 in Apache 2 SAPI mode. If Apache does not generate this value, PHP generates and puts the value in the SCRIPT_FILENAME server constant. This modification complies with CGI specifications. PATH_TRANSLATED exists only under the conditions defined by PATH_INFO.
Apache 2 users can use AcceptPathInfo On in httpd. conf to define PATH_INFO.
"SCRIPT_NAME"
The path that contains the current script. This is useful when the page needs to point to itself. _ FILE _ contains the absolute path and FILE name of the current FILE (for example, including the FILE ).
"REQUEST_URI"
The URI required to access this page. For example, "/index.html ".
"PHP_AUTH_DIGEST"
When running as an Apache module and performing HTTP Digest Authentication, this variable is set to the "Authorization" HTTP header content sent by the client (for further authentication ).
"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 input username.
"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.
"AUTH_TYPE"
When PHP runs in the Apache module mode and uses the HTTP authentication function, this variable is the authentication type.

Environment variable: $ _ ENV
Note: PHP 4.1.0 and later versions are used. In earlier versions, $ HTTP_ENV_VARS is used.
When the parser is running, these variables change from environment variables to PHP global variable namespace ). Many of them are determined by the system running PHP. A complete list is impossible. Check the system documentation to determine its specific environment variables.
Other environment variables (including CGI variables), whether PHP is run as a server module or as a CGI, are listed here.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ _ ENV; to access a function or method, just like using $ HTTP_ENV_VARS.
$ HTTP_ENV_VARS contains the same information, but it is not an automatic global variable (note: $ HTTP_ENV_VARS and $ _ ENV are different variables, and PHP processes them differently ).
If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ ENV and $ HTTP_ENV_VARS arrays are separated. For more information, see the security section using Register Globals. These independent global variables are not automatic global variables.
HTTP Cookies: $ _ COOKIE
Note: PHP 4.1.0 and later versions are used. In earlier versions, $ HTTP_COOKIE_VARS is used.
An array composed of variables passed through HTTP cookies. Is an automatic global variable.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ _ COOKIE; to access a function or method, just like using $ HTTP_COOKIE_VARS.
$ HTTP_COOKIE_VARS contains the same information, but it is not an automatic global variable (note: $ HTTP_COOKIE_VARS and $ _ COOKIE are different variables, and PHP processes them differently ).
If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ COOKIE and $ HTTP_COOKIE_VARS arrays are separated. For more information, see the security section using Register Globals. These independent global variables are not automatic global variables.
Http get variable: $ _ GET
Note: PHP 4.1.0 and later versions are used. In earlier versions, $ HTTP_GET_VARS is used.
An array composed of variables passed through the http get method. Is an automatic global variable.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ _ GET; to access a function or method, just like using $ HTTP_GET_VARS.
$ HTTP_GET_VARS contains the same information, but it is not an automatic global variable (note: $ HTTP_GET_VARS and $ _ GET are different variables, and PHP processes them differently ).
If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ GET and $ HTTP_GET_VARS arrays are separated. For more information, see the security section using Register Globals. These independent global variables are not automatic global variables.
Http post variable: $ _ POST
Note: PHP 4.1.0 and later versions are used. In earlier versions, $ HTTP_POST_VARS is used.
An array composed of variables passed through the http post method. Is an automatic global variable.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ _ POST; to access a function or method, just like using $ HTTP_POST_VARS.
$ HTTP_POST_VARS contains the same information, but it is not an automatic global variable (note: $ HTTP_POST_VARS and $ _ POST are different variables, and PHP processes them differently ).
If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ POST and $ HTTP_POST_VARS arrays are separated. For more information, see the security section using Register Globals. These independent global variables are not automatic global variables.
HTTP file Upload variable: $ _ FILES
Note: PHP 4.1.0 and later versions are used. In earlier versions, $ HTTP_POST_FILES is used.
An array composed of uploaded File items passed through the http post method. Is an automatic global variable.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ _ FILES; to access a function or method, just like using $ HTTP_POST_FILES.
$ HTTP_POST_FILES contains the same information, but it is not an automatic global variable (note that PHP treats the $ HTTP_POST_FILES and $ _ FILES variables as different variables ).
If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ FILES and $ HTTP_POST_FILES arrays are separated. For more information, see the security section using Register Globals. These independent global variables are not automatic global variables.
Request variable: $ _ REQUEST
Note: PHP 4.1.0 and later versions are used. In earlier versions, there is no equivalent array.
Note: Before PHP 4.3.0, $ _ FILES were included in the $ _ REQUEST array.
This Associated array contains all the content in $ _ GET, $ _ POST, and $ _ COOKIE.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ _ REQUEST; to access a function or method.
If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ REQUEST array is separated. For more information, see the security section using Register Globals. These independent global variables are not automatic global variables.
Session variable: $ _ SESSION
Note: PHP 4.1.0 and later versions are used. In earlier versions, $ HTTP_SESSION_VARS is used.
An array containing the session variables in the current script. See the Session function documentation for more information.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ _ SESSION; to access a function or method, just like using $ HTTP_SESSION_VARS.
$ HTTP_SESSION_VARS contains the same information, but it is not an automatic global variable (note that PHP treats the $ HTTP_SESSION_VARS and $ _ SESSION variables as different variables ).
If the register_globals command is set, these variables are also available in all scripts; that is, the $ _ SESSION and $ HTTP_SESSION_VARS arrays are separated. For more information, see the security section using Register Globals. These independent global variables are not automatic global variables.
Global variable: $ GLOBALS
Note: $ GLOBALS is applicable to PHP 3.0.0 and later versions.
An array composed of all defined global variables. The variable name is the index of the array.
This is a "superglobal", or it can be described as an automatic global variable. This only means that it is valid in all scripts. You do not need to use global $ GLOBALS; to access a function or method.
Previous error message: $ php_errormsg
$ Php_errormsg is a variable that contains the content of the previous error message generated by PHP. This variable is valid only when an error occurs and the track_errors option is enabled (disabled by default.
The code is as follows:
Function getServerAddress (){
If ($ _ SERVER ['server _ ADDR ']) {
Return $ _ SERVER ['server _ ADDR '];
}

$ Ifconfig = shell_exec ('/sbin/ifconfig eth0 ');
Preg_match ('/addr :( [\ d \.] +)/', $ ifconfig, $ match );

Return $ match [1];
}
?>

The code is as follows:
Function getServerAddress (){
If ($ _ SERVER ['server _ ADDR ']) {
Return $ _ SERVER ['server _ ADDR '];
}

$ Ifconfig = shell_exec ('/sbin/ifconfig eth0 ');
Preg_match ('/addr :( [\ d \.] +)/', $ ifconfig, $ match );

Return $ match [1];
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.