Home > Backend Development > PHP Tutorial > PHP predefined variable usage help (with examples)_PHP tutorial

PHP predefined variable usage help (with examples)_PHP tutorial

WBOY
Release: 2016-07-13 10:25:44
Original
856 people have browsed it

As of PHP 4.1.0, the preferred way to obtain external variables is to use the superglobal variables mentioned below. Before this, people either relied on register_globals or long predefined PHP arrays ($HTTP_*_VARS). Since PHP 5.0.0, long-format PHP predefined variables can be masked by setting register_long_arrays.
Server variable: $_SERVER
Note: Used in PHP 4.1.0 and later versions. Previous versions, used $HTTP_SERVER_VARS.
$_SERVER is an array containing information such as header, path 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 large number of these variables are specified in the CGI 1.1 specification, so they should be studied carefully.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts. There is no need to use global $_SERVER; to access it within a function or method, just like using $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 handles them differently).
These variables are also available in all scripts if the register_globals directive is set; that is, the $_SERVER and $HTTP_SERVER_VARS arrays are separated. 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 few of the elements listed below are valid (or have no real meaning) if you run PHP from the command line.
"PHP_SELF"
The file name of the currently executing script, related to the document root. For example, using $_SERVER['PHP_SELF'] in a script with the URL address http://example.com/test.php/foo.bar will result in /test.php/foo.bar. The __FILE__ constant contains the absolute path and file name of the current (i.e. containing) file.
If PHP is run from the command line, this variable has no effect before PHP 4.3.0.
"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 version of the CGI specification used by the server. For example, "CGI/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 information 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".
Note: If the request method is HEAD, the PHP script will abort after sending the header information (this means that after any output is generated, there is no more output buffering).
"REQUEST_TIME"
The timestamp when the request started. Valid since PHP 5.1.0.
“QUERY_STRING”
The string of query (the content after the first question mark ? in the URL).
“DOCUMENT_ROOT”
The document root directory where the currently running script is located. Defined in the server configuration file.
"HTTP_ACCEPT"
Contents of the Accept: header of the current request.
“HTTP_ACCEPT_CHARSET”
The content of the Accept-Charset: header of the current request. For example: "iso-8859-1,*,utf-8".
“HTTP_ACCEPT_ENCODING”
Contents of the Accept-Encoding: header of the current request. For example: "gzip".
“HTTP_ACCEPT_LANGUAGE”
The content of the Accept-Language: header of the current request. For example: "en".
“HTTP_CONNECTION”
Contents of the Connection: header of the current request. For example: "Keep-Alive".
“HTTP_HOST”
Contents of the Host: header of the current request.
“HTTP_REFERER”
The URL address of the previous page that links to the current page. Not all user agents (browsers) will set this variable, and some can also modify HTTP_REFERER manually. Therefore, this variable is not always true.
“HTTP_USER_AGENT”
Contents of the User-Agent: header of the current request. This string indicates information about the user agent accessing this page. A typical example is: Mozilla/4.5 [en] (X11; U; linux 2.2.9 i586). This information can also be obtained using get_browser().
"HTTPS"
Set to a non-empty value if the script is accessed via the HTTPS protocol.
“REMOTE_ADDR”
The IP address of the user who is browsing the current page.
“REMOTE_HOST”
The host name of the user who is browsing the current page. Reverse domain name resolution is based on the user's REMOTE_ADDR.
Note: The web server must be configured to create this variable. For example Apache requires HostnameLookups On in httpd.conf. See gethostbyaddr().
"REMOTE_PORT"
The port used by users to connect to the server.
"SCRIPT_FILENAME"
The absolute pathname of the currently executing script.
Note: If the script is executed in the CLI, as a relative path, such as file.php or ../file.php, $_SERVER['SCRIPT_FILENAME'] will contain the user-specified relative path.

"SERVER_ADMIN"
This value specifies the SERVER_ADMIN parameter in the Apache server configuration file. If the script is running on a virtual host, this value is that of that virtual host.
"SERVER_PORT"
The port used by the server. Default is "80". If using SSL secure connection, this value is the HTTP port set by the user.
"SERVER_SIGNATURE"
A string containing the server version and virtual hostname.
"PATH_TRANSLATED"
The base path of the file system (not the document root) where the current script is located. This is the result after the server has been imaged from a virtual to real path.
Note: After PHP 4.3.2, PATH_TRANSLATED in Apache 2 SAPI mode is no longer implicitly assigned like Apache 1. Instead, if Apache does not generate this value, PHP will generate it itself and put its value into the SCRIPT_FILENAME server constant. middle. This modification adheres to the CGI specification, which states that PATH_TRANSLATED only exists if PATH_INFO is defined.
Apache 2 users can define PATH_INFO using AcceptPathInfo On in httpd.conf.
"SCRIPT_NAME"
Contains the path to 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 (such as an include file).
"REQUEST_URI"
The URI required to access this page. For example, "/index.html".
"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).
“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.
“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.
“AUTH_TYPE”
When PHP is running in Apache module mode and the HTTP authentication function is being used, this variable is the authentication type.

Environment variable: $_ENV
Note: Used in PHP 4.1.0 and later versions. Previous versions, used $HTTP_ENV_VARS.
When the parser is running, these variables are converted from environment variables into the PHP global variable namespace (namespace). Many of them are determined by the system PHP is running on. A complete list is impossible. Check your system's documentation to determine its specific environment variables.
Other environment variables (including CGI variables), whether PHP is running as a server module or as a CGI handler, are listed here.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts. There is no need to use global $_ENV; to access it within a function or method, as you would with $HTTP_ENV_VARS.
$HTTP_ENV_VARS contains the same information, but is not an automatic global variable (note: $HTTP_ENV_VARS and $_ENV are different variables, and PHP handles them differently).
These variables are also available in all scripts if the register_globals directive is set; that is, the $_ENV and $HTTP_ENV_VARS arrays are separated. For related information, see the security-related chapter Using Register Globals. These individual global variables are not automatic global variables.
HTTP Cookies: $_COOKIE
Note: Used in PHP 4.1.0 and later versions. Previous versions, used $HTTP_COOKIE_VARS.
Array of variables passed via HTTP cookies. is an automatic global variable.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts. There is no need to use global $_COOKIE; in a function or method to access it, just like using $HTTP_COOKIE_VARS.
$HTTP_COOKIE_VARS contains the same information, but is not an automatic global variable (note: $HTTP_COOKIE_VARS and $_COOKIE are different variables, and PHP handles them differently).
These variables are also available in all scripts if the register_globals directive is set; that is, the $_COOKIE and $HTTP_COOKIE_VARS arrays are separated. For related information, see the security-related chapter Using Register Globals. These individual global variables are not automatic global variables.
HTTP GET variable: $_GET
Note: Used in PHP 4.1.0 and later versions. Previous versions, used $HTTP_GET_VARS.
An array of variables passed via the HTTP GET method. is an automatic global variable.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts. There is no need to use global $_GET; to access it within a function or method, as you would with $HTTP_GET_VARS.
$HTTP_GET_VARS contains the same information, but is not an automatic global variable (note: $HTTP_GET_VARS and $_GET are different variables, and PHP handles them differently).
These variables are also available in all scripts if the register_globals directive is set; that is, the $_GET and $HTTP_GET_VARS arrays are separated. For related information, see the security-related chapter Using Register Globals. These individual global variables are not automatic global variables.
HTTP POST variable: $_POST
Note: Used in PHP 4.1.0 and later versions. Previous versions, used $HTTP_POST_VARS.
An array of variables passed via the HTTP POST method. is an automatic global variable.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts. There is no need to use global $_POST; to access it within a function or method, as you would with $HTTP_POST_VARS.
$HTTP_POST_VARS contains the same information, but is not an automatic global variable (note: $HTTP_POST_VARS and $_POST are different variables, and PHP handles them differently).
These variables are also available in all scripts if the register_globals directive is set; that is, the $_POST and $HTTP_POST_VARS arrays are separated. For related information, see the security-related chapter Using Register Globals. These individual global variables are not automatic global variables.
HTTP file upload variable: $_FILES
Note: Used in PHP 4.1.0 and later versions. Previous versions, used $HTTP_POST_FILES.
An array of uploaded file items passed via the HTTP POST method. is an automatic global variable.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts.There is no need to use global $_FILES; to access it within a function or method, just like using $HTTP_POST_FILES.
$HTTP_POST_FILES contains the same information, but is not an automatic global variable (please note that PHP treats the two variables $HTTP_POST_FILES and $_FILES as different variables).
These variables are also available in all scripts if the register_globals directive is set; that is, the $_FILES and $HTTP_POST_FILES arrays are separated. For related information, see the security-related chapter Using Register Globals. These individual global variables are not automatic global variables.
Request variable: $_REQUEST
Note: Used in PHP 4.1.0 and later versions. In previous versions, there was no equivalent array.
Note: Prior to PHP 4.3.0, $_FILES was also included in the $_REQUEST array.
This associative array contains everything in $_GET, $_POST and $_COOKIE.
This is a "superglobal", or can be described as an automatic global variable. This simply means that it works in all scripts. There is no need to use global $_REQUEST; within a function or method to access it.
These variables are also available in all scripts if the register_globals directive is set; that is, the $_REQUEST array is detached. For related information, see the security-related chapter Using Register Globals. These individual global variables are not automatic global variables.
session variable: $_SESSION
Note: Used in PHP 4.1.0 and later versions. Previous versions, used $HTTP_SESSION_VARS.
Array containing session variables in the current script. See the Session function documentation for more information.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts. There is no need to use global $_SESSION; in a function or method to access it, just like using $HTTP_SESSION_VARS.
$HTTP_SESSION_VARS contains the same information, but is not an automatic global variable (note that PHP treats $HTTP_SESSION_VARS and $_SESSION as different variables).
These variables are also available in all scripts if the register_globals directive is set; that is, the $_SESSION and $HTTP_SESSION_VARS arrays are separated. For related information, see the security-related chapter Using Register Globals. These individual global variables are not automatic global variables.
Global variable: $GLOBALS
Note: $GLOBALS is available in PHP 3.0.0 and later versions.
An array consisting of all defined global variables. The variable name is the index into the array.
This is a "superglobal", or can be described as an automatic global variable. This just means that it works in all scripts. There is no need to use global $GLOBALS; within a function or method to access it.
Previous error message: $php_errormsg
$php_errormsg is a variable containing the contents of the previous error message generated by PHP. This variable is valid only when an error occurs and the track_errors option is turned on (off by default).

Copy code The code is as follows:

function getServerAddress() {
if($ _SERVER['SERVER_ADDR']) {
          return $_SERVER['SERVER_ADDR']; /addr:([d.]+)/',$ifconfig,$match);

return $match[1];
}
?>




Copy code

The code is as follows:function getServerAddress() {
if($ _SERVER['SERVER_ADDR']) {
          return $_SERVER['SERVER_ADDR']; /addr:([d.]+)/',$ifconfig,$match);

return $match[1];
}
?>






http://www.bkjia.com/PHPjc/824951.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/824951.html

Since PHP 4.1.0, the preferred way to obtain external variables is to use the superglobal variables mentioned below. Before this, people either relied on register_globals or the long list of predefined PHP...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template