downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

$php_errormsg> <$_ENV
[edit] Last updated: Fri, 25 May 2012

view this page in

$_COOKIE

$HTTP_COOKIE_VARS [已弃用]

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

说明

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

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

更新日志

版本 说明
4.1.0 引入 $_COOKIE,弃用 $HTTP_COOKIE_VARS

范例

Example #1 $_COOKIE 范例

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

假设之前发送了 "name" Cookie

以上例程的输出类似于:

Hello Hannes!

注释

Note:

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



add a note add a note User Contributed Notes $_COOKIE
Chris Watson 29-Sep-2009 11:18
The value of $_COOKIE is determined by the content of cookies received in the user agent's request.

If you set a cookie (ie send it to the browser), it won't be sent back until the next request and so the data won't be present in $_COOKIE.
Sam Yong - hellclanner at live dot com 05-Sep-2009 01:27
Take note that in IE when it's really weird that when you do something like this:

<?php

// import config/constants

session_set_cookie_params((time()+$_SITE['session_length']));
session_start();
$sess = session_name();
setcookie($sess, $_COOKIE[$sess], time() + $_SITE['session_length']);

// .. rest of the code

?>

It fails. the session cookie is not stored totally.

Instead, doing this would work:

<?php

// import config/constants

session_set_cookie_params((time()+$_SITE['session_length']));
session_start();
$sess = session_name();
setcookie($sess, session_id(), time() + $_SITE['session_length']);

// .. rest of the code

?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites