PHP Quick Start Learning-12 (Super global variable)

Source: Internet
Author: User
Tags html form php script

PhpSuper Global Variables

The Super global variable is enabled after PHP 4.1.0 and is a self-contained variable in the PHP system and is available in all scopes of a script.

PHP Super Global variables

Several super global variables (superglobals) are predefined in PHP, which means they are available in all scopes of a script. You don't need to specify it, you can use it in functions and classes.

PHP Super Global Variables list:

    • $GLOBALS
    • $_server
    • $_request
    • $_post
    • $_get
    • $_files
    • $_env
    • $_cookie
    • $_session

In this section we will cover several common super global variables, the rest of which we will introduce in the next few chapters.

PHP $GLOBALS

$GLOBALS is a super global variable group of PHP that can be accessed in all scopes of a PHP script.

$GLOBALS is a globally combined array that contains all the variables. The name of the variable is the key of the array.

The following example shows how to use the Super global variable $GLOBALS:

<?  $x =$y =$GLOBALS$GLOBALS  $GLOBALS[' y 'echo$z?>

In the above example, Z is a super global variable in an $globals array that can also be accessed outside the function.

PHP $_server

$_server is an array of information, such as header information (header), path, and script location (scripts locations), and so on. The items in this array are created by the WEB server. There is no guarantee that each server will provide all the items, the server may ignore some, or provide some items that are not listed here.

The following examples show how to use the elements in $_server:

<?PHPEcho $_server[' Php_self '];Echo"<br>";Echo $_server[' server_name '];Echo"<br>";Echo $_server[' Http_host '];Echo"<br>";Echo $_server[' Http_referer '];Echo"<br>";Echo $_server[' Http_user_agent '];Echo"<br>";Echo $_server[' Script_name '];?>

The following table lists the important elements in all $_server variables:

element/code Description
$_server[' Php_self '] The file name of the current execution script, which is related to document root. For example, using $_server[' php_self ' in a script with an address of Http://example.com/test.php/foo.bar will get/test.php/foo.bar. The __FILE__ constant contains the full path and file name of the current (for example, the containing) file. Starting with PHP 4.3.0, if PHP is running in command-line mode, this variable will contain the script name. Previous versions of this variable are not available.
$_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 by that virtual host. (eg: www.runoob.com)
$_server[' Server_software '] The server identification string, given in the header information in response to the request. (eg: apache/2.2.24)
$_server[' Server_protocol '] The name and version of the communication protocol when the page is requested. For example, "http/1.0".
$_server[' Request_method '] Access the request method used by the page; for example, "GET", "HEAD", "POST", "PUT".
$_server[' Request_time '] Timestamp at the start of the request. Available from PHP 5.1.0. (eg: 1377687496)
$_server[' query_string '] Query string, if any, for page access.
$_server[' Http_accept '] Accept: The contents of the item, if any, in the current request header.
$_server[' Http_accept_charset '] Accept-charset in the current request header: The contents of the item, if one exists. For example: "Iso-8859-1,*,utf-8".
$_server[' Http_host '] Host in current request header: The contents of the item, if one exists.
$_server[' Http_referer '] The address, if any, that directs the user's agent to the previous page of the current page. Determined by the user agent settings. Not all user agents will set the item, and some also provide the ability to modify the Http_referer. In short, the value is not trustworthy. )
$_server[' HTTPS '] If the script is accessed through the HTTPS protocol, it is set to a non-null value.
$_server[' REMOTE_ADDR '] The IP address of the user who browsed the current page.
$_server[' Remote_host '] The host name of the user who browsed the current page. DNS reverse resolution is not dependent on the user's remote_addr.
$_server[' Remote_port '] The port number used by the user's machine to connect to the WEB server.
$_server[' Script_filename '] The absolute path of the current execution script.
$_server[' Server_admin '] This value indicates the Server_admin parameter in the Apache server configuration file. If the script is running on a virtual host, the value is the value of that virtual host. (such as: [email protected])
$_server[' Server_port '] The port used by the WEB server. The default value is "80". If SSL secured connection is used, this value is the HTTP port set by the user.
$_server[' Server_signature '] A string that contains the server version and the virtual host name.
$_server[' path_translated '] The base path of the file system (not the document root) where the current script resides. This is the result of the server making a virtual-to-real-path image.
$_server[' Script_name '] Contains the path to 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 the containing file.
$_server[' Script_uri '] The 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 (form) of an input field and a Submit button (submit). When the user submits the form data by clicking the "Submit" button, the form data is sent to the script file specified in the Action property in the <form> tab. In this example, we specify the file to process the form data. If you want other PHP files to handle this data, you can modify the specified script file name. We can then use the Super global variable $_request to collect input field data from the form:

12<body>3 4<form method= "POST" action= "<?php Echo$_server[' php_self '];? > ">5Name: <input type= "text" name= "fname" >6<input type= "Submit" >7</form>8 9<?PHPTen $name=$_request[' FName '];  One Echo $name;  A?> -  -</body> thePHP $_post

PHP $_post is widely used to collect form data, specifying this attribute in the HTML form tag: "method=" POST ".

The following example shows a form (form) of an input field and a Submit button (submit). When the user submits the form data by clicking the "Submit" button, the form data is sent to the script file specified in the Action property in the <form> tab. In this example, we specify the file to process the form data. If you want other PHP files to handle this data, you can modify the specified script file name. We can then use the Super global variable $_post to collect input field data from the form:

12<body>3 4<form method= "POST" action= "<?php Echo$_server[' php_self '];? > ">5Name: <input type= "text" name= "fname" >6<input type= "Submit" >7</form>8 9<?PHPTen $name=$_post[' FName '];  One Echo $name;  A?> -  -</body> thePHP $_get

PHP $_get is also widely used to collect form data, specifying this attribute in the HTML form tag: "method=" GET ".

$_get can also collect the data sent in the URL.

Suppose we have a hyperlink HTML page that contains parameters:

1 2 <body>34$GET</a>56 </body>7 

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 the data.

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

 echo$_get$_get[' web '];? ></body>

PHP Quick Start Learning-12 (Super global variable)

Related Article

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.