Home > Article > Backend Development > How many php super global variables are there?
There are 9 php super global variables, namely: "$GLOBALS", "$_SERVER", "$_GET", "$_POST", "$_FILES", "$_COOKIE", "$_SESSION" ”, “$_REQUEST”, “$_ENV”.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Many predefined variables in PHP are " "superglobal", which means they are available throughout the entire scope of a script. They can be accessed within a function or method without executing global $variable;
.
These superglobal variables are:
$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION
$_REQUEST
$_ENV
PHP $_GET can also be used to collect form data after submitting an HTML form (method="get").
$_GET can also collect data sent in the URL.
For example, access the URL link:
http://localhost/test_get.php?subject=PHP&web=W3school.com.cn
The usage is as follows:
<html> <body> <?php echo "Study " . $_GET['subject'] . " at " . $_GET['web']; ?> </body> </html>
PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also commonly used to pass variables.
The following example shows a form containing input fields and a submit button. When the user clicks the submit button to submit the data, the form data is sent to the file specified in the action attribute of the ff9c23ada1bcecdd1a0fb5d5a0f18437 tag. In this example, we specify the file itself to handle the form data. If you wish to use another PHP page to handle the form data, change the file name to a file name of your choice. Then, we can use the super global variable $_POST to collect the value of the input field:
<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>
PHP $_REQUEST is used to collect data submitted by HTML forms.
The following example shows a form containing input fields and a submit button. 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 itself to handle the form data. If you need to use other PHP files to process form data, please change the file name to the file name of your choice. Then, we can use the super global variable $_REQUEST to collect the value of the input field:
<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>
- $GLOBALS This type of global variable is used to access global variables from anywhere in a PHP script (either from a function or method).
- PHP stores all global variables in an array named $GLOBALS[index]. The name of the variable is the key of the array.
- Mainly used when global variables cannot be used in the local scope, but the global variables need to be used, use $GLOBALS.
The following example shows how to use the super global variable $GLOBALS:
<?php $x = 75; $y = 25; function addition() { $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; } addition(); echo $z; ?>
- $_FILES is mainly used when a binary file needs to be uploaded. To upload an abc.mp3 file, the server needs to obtain relevant information about the file, which is obtained through the variable $_FILES.
- $_FILES The super global variable contains information about the data uploaded to the server through the POST method. This super global variable is different from other variables. It is a two-dimensional array containing 5 elements.
Element/Code | Description |
---|---|
$ _FILES['userfile']['name'] | The original name of the client machine file |
$_FILES['userfile']['type'] | The MIME type of the file requires the browser to provide support for this information, such as "image/gif" |
The size of the uploaded file, in bytes | |
After the file is uploaded, it will be served The name of the temporary file stored on the end | |
and the error code related to the file upload. [‘error’] was added in PHP version 4.2.0. Although this variable is named error , this variable is actually filled in on success. It has five possible values: see note ③ |
元素/代码 | 描述 |
---|---|
$_SERVER[‘PHP_SELF’] | 返回当前执行脚本的文件名。 |
$_SERVER[‘GATEWAY_INTERFACE’] | 返回服务器使用的 CGI 规范的版本。 |
$_SERVER[‘SERVER_ADDR’] | 返回当前运行脚本所在的服务器的 IP 地址。 |
$_SERVER[‘SERVER_NAME’] | 返回当前运行脚本所在的服务器的主机名(比如 www.w3school.com.cn)。 |
$_SERVER[‘SERVER_SOFTWARE’] | 返回服务器标识字符串(比如 Apache/2.2.24)。 |
$_SERVER[‘SERVER_PROTOCOL’] | 返回请求页面时通信协议的名称和版本(例如,“HTTP/1.0”)。 |
$_SERVER[‘REQUEST_METHOD’] | 返回访问页面使用的请求方法(例如 POST)。 |
$_SERVER[‘REQUEST_TIME’] | 返回请求开始时的时间戳(例如 1577687494)。 |
$_SERVER[‘QUERY_STRING’] | 返回查询字符串,如果是通过查询字符串访问此页面。 |
$_SERVER[‘HTTP_ACCEPT’] | 返回来自当前请求的请求头。 |
$_SERVER[‘HTTP_ACCEPT_CHARSET’] | 返回来自当前请求的 Accept_Charset 头( 例如 utf-8,ISO-8859-1) |
$_SERVER[‘HTTP_HOST’] | 返回来自当前请求的 Host 头。 |
$_SERVER[‘HTTP_REFERER’] | 返回当前页面的完整 URL(不可靠,因为不是所有用户代理都支持)。 |
$_SERVER[‘HTTPS’] | 是否通过安全 HTTP 协议查询脚本。 |
$_SERVER[‘REMOTE_ADDR’] | 返回浏览当前页面的用户的 IP 地址。 |
$_SERVER[‘REMOTE_HOST’] | 返回浏览当前页面的用户的主机名。 |
$_SERVER[‘REMOTE_PORT’] | 返回用户机器上连接到 Web 服务器所使用的端口号。 |
$_SERVER[‘SCRIPT_FILENAME’] | 返回当前执行脚本的绝对路径。 |
$_SERVER[‘SERVER_ADMIN’] | 该值指明了 Apache 服务器配置文件中的 SERVER_ADMIN 参数。 |
$_SERVER[‘SERVER_PORT’] | Web 服务器使用的端口。默认值为 “80”。 |
$_SERVER[‘SERVER_SIGNATURE’] | 返回服务器版本和虚拟主机名。 |
$_SERVER[‘PATH_TRANSLATED’] | 当前脚本所在文件系统(非文档根目录)的基本路径。 |
$_SERVER[‘SCRIPT_NAME’] | 返回当前脚本的路径。 |
$_SERVER[‘SCRIPT_URI’] | 返回当前页面的 URI。 |
PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置。Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用。
- 当您运行一个应用程序时,您会打开它,做些更改,然后关闭它。这很像一次会话。计算机清楚你是谁。它知道你何时启动应用程序,并在何时终止。但是在因特网上,存在一个问题:服务器不知道你是谁以及你做什么,这是由于 HTTP 地址不能维持状态。
- 通过在服务器上存储用户信息以便随后使用,PHP session 解决了这个问题(比如用户名称、购买商品等)。不过,会话信息是临时的,在用户离开网站后将被删除。如果您需要永久储存信息,可以把数据存储在数据库中。
- Session 的工作机制是:为每个访问者创建一个唯一的 id (UID),并基于这个 UID 来存储变量。UID 存储在 cookie 中,亦或通过 URL 进行传导。
①开始 PHP Session :
在您把用户信息存储到 PHP session 中之前,首先必须启动会话。
注释:session_start() 函数必须位于 标签之前
<?php session_start(); ?> <html> <body> </body> </html>
②存储 和使用Session 变量:
<?php session_start(); // store session data $_SESSION['views']=1; ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html>
③终结 Session
如果您希望删除某些 session 数据,可以使用 unset() 或 session_destroy() 函数。
//通过 unset() 函数用于释放指定的 session 变量: <?php unset($_SESSION['views']); ?> //通过 session_destroy() 函数彻底终结 session: <?php session_destroy(); ?>
注释:session_destroy() 将重置 session,您将失去所有已存储的 session 数据。
cookie 常用于识别用户。cookie 是服务器留在用户计算机中的小文件。每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。
①创建 Cookie
//语法 setcookie(name, value, expire, path, domain); //示例 <?php setcookie("user", "Alex Porter", time()+3600); //创建名为 "user" 的 cookie,把为它赋值 "Alex Porter"。我们也规定了此 cookie 在一小时后过期 ?> <html> <body> </body> </html>
②取回 Cookie 值
//示例a:取回了名为 "user" 的 cookie 的值,并把它显示在了页面上 <?php // Print a cookie echo $_COOKIE["user"]; // A way to view all cookies print_r($_COOKIE); ?> //示例b:使用 isset() 函数来确认是否已设置了 cookie <html> <body> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br />"; else echo "Welcome guest!<br />"; ?> </body> </html>
PHP中的$_ENV是一个包含服务器端环境变量的数组,不同系统不完全一样。
部分变量示例:
$_ENV[ ‘HOSTNAME’ ] 服务器的主机名
$_ENV[ ‘SHELL’ ] 系统 shell
$_ENV只是被动的接受服务器端的环境变量并把它们转换为数组元素,你可以尝试直接输出它:
//输出内容格式清晰,ThinkPHP可以直接用dump() var_dump($_ENV); //输出到屏幕 print_r($_ENV); //输出key-value键值对 foreach($_ENV as $key=>$val){echo $key.'--------'.$val.'<br>';}
推荐学习:《PHP视频教程》
The above is the detailed content of How many php super global variables are there?. For more information, please follow other related articles on the PHP Chinese website!