PHP super global variables

Super global variables were enabled after PHP 4.1.0. They are variables that come with the PHP system and are available in the entire scope of a script.

PHP super global variables

Several super global variables (superglobals) are predefined in PHP, which means that they are in a script Available in all scopes. You can use it in functions and classes without special instructions.

PHP super global variable list:

• $GLOBALS stores all global variables in the current script, whose KEY is the variable name and VALUE is the variable value

• $_SERVER current Web server variable array

• $_REQUEST stores all request arrays in the submission form, including everything in $_GET, $_POST, $_COOKIE, and $_SESSION

• $_POST stores The data in the form submitted by the POST method

• $_GET stores the data in the form submitted by the GET method

• $_FILES stores the data submitted by the uploaded file to the current script

• $_ENV stores the current Web environment variables

• $_COOKIE gets or sets the variable array stored in the user's browser cookies

• $_SESSION stores the session variable array of the current script

In this chapter we will explain several commonly used super global variables, and we will introduce the remaining variables in the next few chapters.

PHP $GLOBALS

$GLOBALS is a super global variable group of PHP that can be accessed in the entire scope of a PHP script .

$GLOBALS is a global combined array containing all variables. The name of the variable is the key of the array.

The following example introduces how to use the super global variable $GLOBALS:

Example

Running example»

In the above example, z is an array of $GLOBALS Super global variable in , this variable can also be accessed outside the function.

PHP $_SERVER

$_SERVER is a server that contains information such as header, path, and script location. locations) and other information. The items in this array are created by the web server. There is no guarantee that every server will offer all items; the server may ignore some, or serve items not listed here.

The following example shows how to use elements in $_SERVER:

Example

"; echo $_SERVER['SERVER_NAME']; echo "
"; echo $_SERVER['HTTP_HOST']; echo "
"; echo $_SERVER['HTTP_REFERER']; echo "
"; echo $_SERVER['HTTP_USER_AGENT']; echo "
"; echo $_SERVER['SCRIPT_NAME']; ?>

Running example»

The following table lists Important elements in all $_SERVER variables:


##Elements/Code

Description

##

$_SERVER['PHP_SELF'] The file name of the currently executing script, related to the document root. For example, using $_SERVER['PHP_SELF'] in a script at http://example.com/test.php/foo.bar will get / test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. containing) file. Starting from PHP 4.3.0, if PHP runs in command line mode, this variable will contain script names. This variable is not available in previous versions.

$_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 script is currently running.

$_SERVER['SERVER_NAME'] The host name of the server 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. (Such as: FTWARE'] Server identification string, given in the header information when responding to the request. (For example: Apache/2.2.24)

$_SERVER['SERVER_PROTOCOL'] The name and version of the communication protocol when requesting the page. For example, "HTTP/1.0".

$_SERVER['REQUEST_METHOD'] The request method used to access the page; for example, "GET", "HEAD", "POST", "PUT".

$ _ Server ['Request_time'] Request the time stamp at the beginning. Available since PHP 5.1.0. (eg: 1377687496)

$_SERVER['QUERY_STRING'] Query string (query string), if any, page access is performed through it.

$_SERVER['HTTP_ACCEPT'] The content of the Accept: item in the current request header, if it exists.

$_SERVER['HTTP_ACCEPT_CHARSET'] The content of the Accept-Charset: item in the current request header, if it exists. For example: "iso-8859-1,*,utf-8".

$_SERVER['HTTP_HOST'] , to be the content of the Host: item in the current request header, if it exists.

$_SERVER['HTTP_REFERER'] being directed to the address of the previous page of the current page (if one exists). Determined by user agent settings. Not all user agents will set this item, and some also provide the function of modifying HTTP_REFERER. In short, the value is not trustworthy. )

$_SERVER['HTTPS'] to be set to a non-empty value if the script is accessed via the HTTPS protocol.

$_SERVER['REMOTE_ADDR'] The IP address of the user browsing the current page.

$ _ server ['remote_host'] Browse the host name of the user on the current page. DNS reverse resolution does not depend on the user's REMOTE_ADDR.

$ _ Server ['remote_port'] connected to the port number used on the web server on the user machine.

$_SERVER['SCRIPT_FILENAME'] The absolute path of the currently executing script.

$_SERVER['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 the value for that virtual host. (eg: someone@runoob.com)

$_SERVER['SERVER_PORT'] The port used by the web server. The default value is "80". 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['PATH_TRANSLATED'] The base path of the file system (not the document root directory) where the current script is located. This is the result after the server has been imaged from a virtual to real path.

$_SERVER['SCRIPT_NAME'] Contains the path of the current script. This is useful when the page needs to point to itself. The __FILE__ constant contains the full path and file name of the current script (such as an include file).

$_SERVER['SCRIPT_URI'] URI is used to specify the page to be accessed. For example "/index.html".


PHP $_REQUEST

##PHP $_REQUEST is used to collect data submitted by HTML forms.

The following example shows a form with an input field (input) and a submit button (submit). When a user submits form data by clicking the "Submit" button, the form data is sent to the script file specified in the action attribute of the
tag. In this example, we specify the file to handle the form data. If you want another PHP file to handle this data, you can modify the specified script file name. Then, we can use the super global variable $_REQUEST to collect input field data in the form:

Instance

   Name:      

Run Example»


$_SERVER['PHP_SELF '] is often used during development. It is generally used to reference the current web page address, and it is a global variable automatically generated by the system.


PHP $_POST##PHP $_POST is widely used to collect form data in HTML form Specify this attribute of the tag: "method="post".

The following example shows a form with an input field (input) and a submit button (submit). When the user submits by clicking the "Submit" button When forming form data, the form data will be sent to the script file specified in the action attribute of the

tag. In this example, we specify the file to handle the form data. If you want another PHP file to handle the data, you can. Modify the specified script file name. Then, we can use the super global variable $_POST to collect the input field data in the form:

Example

   Name:      

Run Example»



PHP $_GET

PHP $_GET is also widely used to collect form data. Specify this attribute in the HTML form tag: "method="get".

$_GET can also collect data sent in the URL.

Suppose we have a hyperlinked HTML page containing parameters:

  Test $GET  

When the user clicks on the link "Test $GET", the parameters "subject" and "web" will be sent to "test_get.php", You can use the $_GET variable in the "test_get.php" file to get this data

The following example shows the code of the "test_get.php" file:

Example

    

Running Example»

Tip: If you want to learn more about $_POST and $_GET, please visit our PHP Forms chapter


#. #

Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!