Superglobals are specially-defined array variables in PHP that make it easy for you to get information about a request or its context. They are called superglobal because they are always accessible, regardless of the scope — that is, you can access them from any function, class or file without having to do anything special. The superglobal variables are: $GLOBALS
, $_SERVER
, $_GET
, $_POST
, $_FILES
, $_COOKIE
, $_SESSION
, $_REQUEST
and $_ENV
. And while you can learn more about each by reading the PHP documentation, I’d like to show you the ones I think you’re likely use the most.
超全局变量是PHP中特别定义的数组变量,可让您轻松获取有关请求或其上下文的信息。 它们之所以被称为超全局变量,是因为它们始终可以访问,而不管其范围是什么,也就是说,您可以从任何函数,类或文件中访问它们,而无需执行任何特殊操作。 超全局变量是: $GLOBALS
, $_SERVER
, $_GET
, $_POST
, $_FILES
, $_COOKIE
, $_SESSION
, $_REQUEST
和$_ENV
。 尽管您可以通过阅读PHP文档来了解更多信息,但我想向您展示我认为您最常使用的内容。
$_GET
and $_POST
are used to get information sent in the page request. Parameters sent in the URL are made available by $_GET
. For example, this URL:
$_GET
和$_POST
用于获取页面请求中发送的信息。 URL中发送的参数由$_GET
。 例如,此URL:
http://example.com/profile.php?uid=12345
… will send the parameter uid
to profile.php
, which you can then access the value in profile.php
as $_GET["uid"]
.
…会将参数uid
发送到profile.php
,然后可以使用$_GET["uid"]
来访问profile.php
的值。
When you have a form submission that is sent using method="get"
, the values are appended to the URL so they are available using $_GET as well
. Forms submitted using method="post"
send their values in the body of the HTTP request; posted data is available in your script by using $_POST
. Here’s a sample web form which posts a username and password to a login script:
当您使用method="get"
发送表单提交时,这些值将附加到URL上,因此也可以使用$_GET as well
使用。 使用method="post"
提交的表单将其值发送到HTTP请求的正文中; 使用$_POST
可在脚本中提供已发布的数据。 这是一个示例Web表单,该表单将用户名和密码发布到登录脚本中:
<form action="login.php" method="post">
Username: <input type="text" name="uname"><br>
Password: <input type="password" name="pass"><br>
<input type="submit" value="Login">
</form>
The uname
value and pass
value will be available as $_POST["uname"]
and $_POST["pass"]
respectively in login.php
.
uname
值和pass
值将分别在login.php
以$_POST["uname"]
和$_POST["pass"]
提供。
The $_REQUEST
superglobal is an amalgam of $_POST
and $_GET
data. It’s offered as a convenience, but as a general rule I advice people to avoid it. It creates some of the same security risks that registered globals did years ago if you’re not careful. (If you don’t know what I mean by registered globals, consider yourself lucky. Just forget about $_REQUEST
and you’ll be fine.)
$_REQUEST
超全局变量是$_POST
和$_GET
数据的混合物。 提供它是为了方便,但作为一般规则,我建议人们避免使用它。 如果您不小心,它会带来一些与注册全球人员几年前一样的安全风险。 (如果您不了解注册全球人士的含义,请认为自己很幸运。只需忘掉$_REQUEST
,您就可以了。)
The HTTP protocol, the mechanism by which browsers request pages and receive them, was originally designed so that each request was independent from all the others made to the server. This isn’t how we use the web today, though. Now people expect each page they visit in a site to “remember” what happened previously, whether it’s logging in or adding a new video game to their online shopping cart. This logical link between pages is called a session.
HTTP协议是浏览器请求页面并接收页面的机制,其最初设计是使每个请求都独立于对服务器的所有其他请求。 但是,今天这不是我们使用网络的方式。 现在,人们希望他们访问网站的每个页面都能“记住”以前发生的情况,无论是登录还是在其在线购物车中添加了新的视频游戏。 页面之间的逻辑链接称为会话 。
To enable sessions, the session_start()
function must be called at the top of any PHP script using sessions before output is sent to the browser. Once the session is active, you can store and retrieve data using $_SESSION
. Here’s an example…
要启用会话,必须在使用会话的任何PHP脚本的顶部调用session_start()
函数,然后才能将输出发送到浏览器。 会话激活后,您可以使用$_SESSION
存储和检索数据。 这是一个例子……
page1.php
starts the session, stores a value in the $_SESSION
array and then provides a link to page2.php
:
page1.php
启动会话,将值存储在$_SESSION
数组中,然后提供指向page2.php
的链接:
<?php
session_start();
$_SESSION["videogame"] = "Battletoads";
?>
<a href="page2.php">Go to page 2</a>
When the user clicks on the link to page2.php
, it calls session_start()
to resume the session, retrieves the value from $_SESSION["videogame"]
and displays it:
当用户单击page2.php
的链接时,它将调用session_start()
恢复会话,并从$_SESSION["videogame"]
检索值并显示:
<?php
session_start();
echo $_SESSION["videogame"];
The $_SERVER
superglobal array stores information about both the web server and the request made to it. Some of the more useful $_SERVER
values are:
$_SERVER
超全局数组存储有关Web服务器和对其的请求的信息。 一些更有用的$_SERVER
值是:
$_SERVER["PHP_SELF"]
– the name of the currently running PHP script
$_SERVER["PHP_SELF"]
–当前正在运行PHP脚本的名称
$_SERVER["REQUEST_METHOD"]
– which HTTP method was used to request the page (GET
, POST
, etc)
$_SERVER["REQUEST_METHOD"]
–使用哪种HTTP方法请求页面( GET
, POST
等)
$_SERVER["REQUEST_TIME"]
– the time the page request was received, represented as an integer known as a unix timestamp
$_SERVER["REQUEST_TIME"]
-接收页面请求的时间,表示为一个称为unix时间戳的整数
$_SERVER["HTTP_REFERER"]
– the address of the previously visited page, if available
$_SERVER["HTTP_REFERER"]
–先前访问的页面的地址(如果有)
A list of all the available keys can be found in the online documentation. Keep in mind that not every one of them listed is guaranteed to be in $_SERVER
; it depends on your server configuration and the request sent by the user.
可以在联机文档中找到所有可用键的列表。 请记住,并非所有列出的列表都保证在$_SERVER
; 这取决于您的服务器配置和用户发送的请求。
As I said, these are just a few of the superglobals available in PHP which I think you’ll use the most. $_GET
and $_POST
allow your script to take in information from the outside world. $_SESSION
lets you remember data between page requests during a visitor’s browsing session. $_SERVER
holds information about the web server and the page request. Understanding these and learning to use them effectively will no doubt help you write great, interactive websites using PHP with relative ease.
正如我所说,这些只是PHP中可用的超全局变量中的少数,我认为您会使用最多。 $_GET
和$_POST
允许您的脚本从外部接收信息。 $_SESSION
使您可以在访问者的浏览会话期间记住页面请求之间的数据。 $_SERVER
包含有关Web服务器和页面请求的信息。 理解并学习有效使用它们无疑将帮助您相对轻松地使用PHP编写出色的交互式网站。
Image via Tom Wang / Shutterstock
图片来自Tom Wang / Shutterstock