Latest web development tutorials

PHP superglobal

Super global variables are enabled after PHP 4.1.0, the PHP system comes with variables in a script of all scopes are available.


PHP superglobal

PHP predefined several super global variables (superglobals), which means that they all scopes in a script are available. You do not need special instructions, you can use the functions and classes.

PHP super global variable list:

  • $ GLOBALS
  • $ _SERVER
  • $ _REQUEST
  • $ _POST
  • $ _GET
  • $ _FILES
  • $ _ENV
  • $ _COOKIE
  • $ _SESSION

This section will explain several popular super global variable, the variable will be introduced to the rest of the next few chapters.


PHP $ GLOBALS

$ GLOBALS is a super set of PHP global variables in all scopes of a PHP script can access.

$ GLOBALS is a composition that contains an array of all global variables. Name of the variable is an array of keys.

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

Examples

<?php
$x = 75;
$y = 25;
 
function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
 
addition();
echo $z;
?>

Running instance »

Z above example is a $ GLOBALS array super-global variable that can also be accessed outside the function.


PHP $ _SERVER

$ _SERVER Is included as a header information (header), an array of paths (path), as well as the location of the script (script locations), etc. information. This array of items created by the Web server. Each server can not guarantee that all items are available; servers may omit some, or provide some programs not listed here out.

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

Examples

<?php
echo $_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'];
?>

Running instance »

The following table lists all the variables $ _SERVER important elements:

Element / Code description
$ _SERVER [ 'PHP_SELF'] The currently executing script file name, and the relevant document root. For example, use $ _SERVER at address http://example.com/test.php/foo.bar script [ 'PHP_SELF'] will be /test.php/foo.bar. __FILE__ Constant contains the full path and filename of the current (for example, comprise) files. From PHP 4.3.0 version, if PHP is running in command line mode, this variable contains the script name. The previous version of the variable is not available.
$ _SERVER [ 'GATEWAY_INTERFACE'] The revision of the CGI specification used by the server; for example, "CGI / 1.1".
$ _SERVER [ 'SERVER_ADDR'] The current IP address of the server running the script is located.
$ _SERVER [ 'SERVER_NAME'] The host name of the server script is current. If the script is running on a virtual host, the name is set by the value of the virtual host that decision. (Eg: www.w3big.com)
$ _SERVER [ 'SERVER_SOFTWARE'] Server identification string, given in the headers when responding to requests in. (Eg: Apache / 2.2.24)
$ _SERVER [ 'SERVER_PROTOCOL'] Request name and version of the communication protocol of the page. For example, "HTTP / 1.0".
$ _SERVER [ 'REQUEST_METHOD'] Request method used to access the page; for example, "GET", "HEAD", "POST", "PUT".
$ _SERVER [ 'REQUEST_TIME'] Request timestamp of the start. 5.1.0 is available from PHP. (Eg: 1377687496)
$ _SERVER [ 'QUERY_STRING'] query string (query string), if any, through it page views.
$ _SERVER [ 'HTTP_ACCEPT'] The current request header Accept: content item, if any.
$ _SERVER [ 'HTTP_ACCEPT_CHARSET'] The current request header Accept-Charset: content item, if any. For example: "iso-8859-1, *, utf-8".
$ _SERVER [ 'HTTP_HOST'] The current request header Host: content item, if any.
$ _SERVER [ 'HTTP_REFERER'] Guide the user agent to the current address of the previous page (if one exists). By the user agent setting. Not all user agents will set this, and some also provide a function to modify HTTP_REFERER. Briefly, this value is not credible. )
$ _SERVER [ 'HTTPS'] If the script is accessed through HTTPS protocol, it was set to a non-null value.
$ _SERVER [ 'REMOTE_ADDR'] View user's IP address of the current page.
$ _SERVER [ 'REMOTE_HOST'] Browse host name of the user of the current page. Reverse DNS does not depend on the user's REMOTE_ADDR.
$ _SERVER [ 'REMOTE_PORT'] Connected to the port number used by the Web server on the user's machine.
$ _SERVER [ 'SCRIPT_FILENAME'] Absolute path of the currently executing script.
$ _SERVER [ 'SERVER_ADMIN'] This value indicates the Apache server configuration file SERVER_ADMIN parameters. If the script is running on a virtual host, then the value defined for that virtual host. (Eg: [email protected])
$ _SERVER [ 'SERVER_PORT'] Port Web server. The default value is "80." If you use SSL secure connection, then this value is set by the user HTTP port.
$ _SERVER [ 'SERVER_SIGNATURE'] String containing the server version and virtual host name.
$ _SERVER [ 'PATH_TRANSLATED'] Base path where the current script file system (not document root directory). This is the result of a server virtual-to-real image after.
$ _SERVER [ 'SCRIPT_NAME'] It contains the current script's path. This is useful for pages which need to point to themselves. Full path and file name __FILE__ constant contains the current script (for example, contains the file).
$ _SERVER [ 'SCRIPT_URI'] URI is used to specify the page you want to access. For example "/index.html".


PHP $ _REQUEST

PHP $ _REQUEST HTML form used to collect the data submitted.

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

Examples

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>

<?php
$name = $_REQUEST['fname'];
echo $name;
?>

</body>
</html>

Running instance »


PHP $ _POST

PHP $ _POST are widely used to collect form data, specify the attribute in HTML form tags: "method =" post ".

The following example shows an input field (input) and the submit button (submit) form (form). When the user submits the form data by clicking on the "Submit" button, the form data is sent to the <form> tag in the script file specified in the action attribute. In this example, we specify a file to process the form data. If you want other PHP files to process the data, you can modify the specified script file name. We can then use the superglobal $ _POST to collect field data in the form of input:

Examples

<Html>
<Body>

<Form method = "post" action = "? <Php echo $ _SERVER [ 'PHP_SELF'];?>">
Name: <input type = "text" name = "fname">
<Input type = "submit">
</ Form>

<? Php
$ Name = $ _POST [ 'fname'];
echo $ name;
?>

</ Body>
</ Html>

Running instance »


PHP $ _GET

PHP $ _GET also been widely used to collect form data, specify the attribute in HTML form tags: "method =" get ".

$ _GET URL can also collect data transmitted.

Suppose we have a parameter that contains a hyperlink HTML page:

<Html>
<Body>

<a href="test_get.php?subject=PHP&web=w3big.com"> Test $ GET </a>

</ Body>
</ Html>

When users click on the link "Test $ GET", the parameter "subject" and "web" is sent to the "test_get.php", you can use the $ _GET variable "test_get.php" file to obtain these data.

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

Examples

<Html>
<Body>

<? Php
echo "Study" $ _GET [ 'subject'] "at" $ _GET [ 'web']...;
?>

</ Body>
</ Html>

Running instance »

Tip: If you want to learn more about the $ _POST and $ _GET knowledge, please visit our PHP form chapters.