$_COOKIE

$HTTP_COOKIE_VARS [已弃用]

$_COOKIE -- $HTTP_COOKIE_VARS [已弃用]HTTP Cookies

说明

通过 HTTP Cookies 方式传递给当前脚本的变量的数组

$HTTP_COOKIE_VARS 包含相同的信息,但它不是一个超全局变量。 (注意 $HTTP_COOKIE_VARS$_COOKIE 是不同的变量,PHP 处理它们的方式不同)

范例

Example #1 $_COOKIE 范例

<?php
echo 'Hello ' htmlspecialchars($_COOKIE["name"]) . '!';
?>

假设之前发送了 "name" Cookie

以上例程的输出类似于:

Hello Hannes!

注释

Note:

"Superglobal"也称为自动化的全局变量。这就表示其在脚本的所有作用域中都是可用的。不需要在函数或方法中用 global $variable; 来访问它。

User Contributed Notes

axodjakov at gmail dot com 07-Jul-2021 11:25
$_COOKIE returns an array if there are more than one cookie saved under the given key.
alexander-schranz at NO_SPAM dot hotmail dot com 12-Feb-2018 06:55
Cookies with the same name the first cookie is used. Clients will send cookies with longer path before cookies with shorter path. This comes from RFC 6265 which says "Cookies with longer paths are listed before cookies with shorter paths.". So you get the best matching cookie for your current request.
kiril (at) atern (dot) us 24-May-2016 12:08
To clarify the previously posted note:

Dots (.) and spaces ( ) in cookie names are being replaced with underscores (_).
k dot andris at gmail dot com 29-May-2015 08:49
beware, dots (.) in cookie names are replaces by underscores (_)