Summary of commonly used predefined variables in php

  • 2020-05-17 04:53:37
  • OfStack

 
<?php 
echo " Current operating system information ".PHP_OS."<br/>"; 
echo ' This file path and file name: '.__FILE__.'<br />'; 
echo " The current PHP Version information ".PHP_VERSION."<br/>"; 
?> 


Commonly used php predefined variables!

The first $_SERVER[PHP_SELF] returns 1 piece of information, such as "/upload/ try.php ", and the second $path_parts = pathinfo(' index.html '); echo $path_parts [' dirname], "\ n"; echo $path_parts [' basename], "\ n"; echo $path_parts [' extension], "\ n"; echo $path_parts [' filename], "\ n"; // since PHP 5.2.0 will return: / uploadindex.htmlhtmlindex
"PHP_SELF"
The file name of the script currently executing, associated with document root. In URL address for http, for example: / / example com/test php/foo bar scripts using $_SERVER [' PHP_SELF] will get/test php/foo bar the result.
"SERVER_PROTOCOL"
The name and version of the communication protocol when the page is requested. For example, "HTTP / 1.0"
REQUEST_METHOD"
The request method when the page is accessed. For example: "GET", "HEAD", "POST", "PUT".
Note: if the request is made in the form of HEAD, the PHP script is aborted after sending the header message (which means that there is no output buffer after any output is produced).

"REQUEST_TIME"
Timestamp at the beginning of the request. Valid from PHP 5.1.0.
"QUERY_STRING"
Query (query) string (the first question mark in URL? The rest).
"DOCUMENT_ROOT"
The document root directory where the script is currently running. Defined in the server configuration file.
"HTTP_ACCEPT"
Current requested Accept: content of header information.
Predefined variable $_SERVER common example
Example: I need to get the current URL address

$url_this = "http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF'];
echo $url_this;


Shows: http: / / localhost lu php

Server variable: $_SERVER

Note: use in PHP 4.1.0 and later. The previous version used $HTTP_SERVER_VARS.

$_SERVER is an array containing such items as header (headers), path (paths), and script location (script locations). The entities of the array are created by the web server. There is no guarantee that all servers will produce all the information; The server may have ignored some information or generated some new information that is not listed below. This means that a large number of these variables are described in CGI 1.1 specification, so you should take a closer look at it.

This is an "superglobal", or automatic global variable. This simply means that it works in all scripts. You do not need to use global $_SERVER in functions or methods; Accessing it is like using $HTTP_SERVER_VARS 1.

$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.)

If the register_globals directive is set, these variables are also available in all scripts; That is, the $_SERVER and $HTTP_SERVER_VARS arrays are separated. For information, see the section on security 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 if PHP is run from the command line, the elements listed below are barely valid (or have no real meaning).


"PHP_SELF"
The file name of the script currently executing, associated with document root. In URL address for http, for example: / / example com/test php/foo bar scripts using $_SERVER [' PHP_SELF] will get/test php/foo bar the result.

If PHP is running on the command line, this variable is invalid.

"argv"
The parameters passed to the script. When the script is run on the command line, the argv variable is passed to the program's C language-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"
Version of the CGI specification used by the server. For example, "CGI/1.1".

'SERVER_NAME'
The name of the server host on which the script is currently running. If the script is running on a virtual host, the name is determined by the value set by that virtual host.

'SERVER_SOFTWARE'
The string of the server identity, given in the header when responding to a request.

"SERVER_PROTOCOL"
The name and version of the communication protocol when the page is requested. For example, "HTTP/1.0".

"REQUEST_METHOD"
The request method when the page is accessed. For example, "GET", "HEAD", "POST", "PUT".

"QUERY_STRING"
The string of the query (query).

"DOCUMENT_ROOT"
The document root directory where the script is currently running. Defined in the server configuration file.

"HTTP_ACCEPT"
Current requested Accept: content of the header.

"HTTP_ACCEPT_CHARSET"
Current requested Accept-Charset: content of the header. For example, "iso-8859-1,*, utf-8".

"HTTP_ACCEPT_ENCODING"
Current requested Accept-Encoding: content of the header. For example, "gzip".

"HTTP_ACCEPT_LANGUAGE"
Current requested Accept-Language: content of the header. For example, "en".

"HTTP_CONNECTION"
Current requested Connection: content of the header. For example, "Keep-Alive".

"HTTP_HOST"
Current requested Host: content of the header.

"HTTP_REFERER"
Link to the URL address for the first 1 page of the current page. Not all user agents (browsers) set this variable, and some can manually modify HTTP_REFERER. Therefore, this variable is not always true.

"HTTP_USER_AGENT"
Current requested User_Agent: content of the header. This string indicates information about the user agent accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586). You can also get this information using ge
owser().

"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 resolution is based on the user's REMOTE_ADDR.

Note: the Web server must be configured to set up this variable. For example, Apache requires HostnameLookups On in httpd.conf. See gethostbyaddr ().

"REMOTE_PORT"
The port used by the user when connecting to the server.

"SCRIPT_FILENAME"
The absolute path name of the script currently executing.

"SERVER_ADMIN"
This value indicates the SERVER_ADMIN parameter in the Apache server configuration file. If the script is running on a virtual host, this value is the value of that virtual host.

"SERVER_PORT"
The port used by the server. The default is "80". If you use the SSL secure connection, this value is the HTTP port you set up.

"SERVER_SIGNATURE"
A string containing the server version and the virtual host name.

"PATH_TRANSLATED"
The basic path to the file system (not the document root) on which the current script resides. This is the result after the server performs a virtual image to the real path.

"SCRIPT_NAME"
Contains the path to the current script. This is useful when the page needs to point to itself.

"REQUEST_URI"
Access the URI required for this page. For example, "/ index.html ".

"PHP_AUTH_USER"
When PHP is running in Apache module mode and is using HTTP authentication, this variable is the username entered by the user.

"PHP_AUTH_PW"
When PHP is running in Apache module mode and is using HTTP authentication, this variable is the password entered by the user.

"AUTH_TYPE"
When PHP is running in Apache module mode and is using HTTP authentication, this variable is the type of authentication.

Related articles: