Php 简明教程
PHP - $_COOKIE
PHP 超全局变量 $_COOKIE 存储通过 HTTP 请求以 cookie 形式传递给当前 PHP 脚本的变量。$HTTP_COOKIE_VARS 也包含相同信息,但它不是超全局变量,现在已被弃用。
The PHP superglobal $_COOKIE stores the variables passed to the current PHP script along with the HTTP request in the form of cookies. $HTTP_COOKIE_VARS also contains the same information, but it is not a superglobal, and it has now been deprecated.
What is a Cookie?
cookie 是由服务器存储在客户端计算机上的文本文件,用于跟踪目的。PHP 透明支持 HTTP cookie。cookie 通常在 HTTP 标头中设置。JavaScript 也可以直接在浏览器上设置 cookie。
Cookies are text files stored by a server on the client computer and they are kept for tracking purpose. PHP transparently supports HTTP cookies. Cookies are usually set in an HTTP header. JavaScript can also sets a cookie directly on a browser.
服务器脚本将一组 cookie 发送到浏览器。它将此信息存储在本地计算机上以供将来使用。下次当浏览器向 Web 服务器发送任何请求时,它会将这些 cookie 信息发送到服务器,而服务器则使用这些信息来识别用户。
The server script sends a set of cookies to the browser. It stores this information on the local machine for future use. Next time, when the browser sends any request to the web server, it sends those cookies information to the server and the server uses that information to identify the user.
The setcookie() Function
PHP 提供 setcookie 函数来创建要随 HTTP 响应一起发送到客户端的 cookie 对象。
PHP provides the setcookie function to create a cookie object to be sent to the client along with the HTTP response.
setcookie(name, value, expire, path, domain, security);
Parameters
-
Name − Name of the cookie stored.
-
Value − This sets the value of the named variable.
-
Expiry − This specifes a future time in seconds since 00:00:00 GMT on 1st Jan 1970.
-
Path − Directories for which the cookie is valid.
-
Domain − Specifies the domain name in very large domains.
-
Security − 1 for HTTPS. Default 0 for regular HTTP.
How to Set Cookies
查看以下 example 。此脚本设置一个名为 username 的 Cookie(如果尚未设置)。
Take a look at the following example. This script sets a cookie named username if it is not already set.
Example
<?php
if (isset($_COOKIE['username'])) {
echo "<h2>Cookie username already set: " . $_COOKIE['username'] . "</h2>";
} else {
setcookie("username", "Mohan Kumar");
echo "<h2>Cookie username is now set.</h2>";
}
?>
从 Apache 服务器的根文档运行此脚本来。您应看到以下消息作为 output −
Run this script from the document root of the Apache server. You should see this message as the output −
Cookie username is now set
如果重新执行此脚本,则现在已设置 Cookie。
If this script is re-executed, the cookie is now already set.
Cookie username already set: Mohan Kumar
How to Remove Cookies
要删除 cookie,请设置具有已经过期的日期的 cookie,以便浏览器触发 cookie 删除机制。
To delete a cookie, set the cookie with a date that has already expired, so that the browser triggers the cookie removal mechanism.
<?php
setcookie("username", "", time() - 3600);
echo "<h2>Cookie username is now removed</h2>";
?>
浏览器现在将显示以下 output −
The browser will now show the following output −
Cookie username is now removed
Setting Cookies Using the Array Notation
您还可以使用 Cookie 名称中的数组符号来设置数组 Cookie。
You may also set the array cookies by using the array notation in the cookie name.
setcookie("user[three]", "Guest");
setcookie("user[two]", "user");
setcookie("user[one]", "admin");
如果 Cookie 名称包含句点 (.),则 PHP 将它们替换为下划线 (_)。
If the cookie name contains dots (.), then PHP replaces them with underscores (_).