PHP – setcookie() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24662580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP – setcookie() not working
提问by Tim
I have this page that sets a cookie and echos out a string if you check a checkbox. The string prints correctly, but the cookie never gets set and I have no idea why.
我有这个页面,它设置了一个 cookie 并在您选中一个复选框时回显出一个字符串。字符串打印正确,但 cookie 永远不会设置,我不知道为什么。
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label for="checkbox">Option 1:</label>
<input type="checkbox" name="checkbox" id="checkbox"><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['checkbox'])) {
setcookie("cookie", "on", time()+3600*24);
echo "You checked the checkbox and a cookie was set with a value of:<br>";
}
else {
setcookie("cookie", "off", time()+3600*24);
echo "You didn't check the checkbox and a cookie was set with a value of:<br>";
}
echo $_COOKIE['cookie'];
?>
Does anyone know why the above code does not work?
有谁知道为什么上面的代码不起作用?
回答by Marc B
PHP superglobals are populated at script start-up time, and then are NOT modified or touched by PHP again for the life of the script. That means $_COOKIE
represents the cookies that were sent to the server in the http request that fired up the script. It will NOT show any cookies you've added/changed/deleted during the life of the script. Those changes will only show up on the NEXT request.
PHP 超全局变量在脚本启动时填充,然后在脚本的生命周期内不会再次被 PHP 修改或触及。这意味着$_COOKIE
表示在启动脚本的 http 请求中发送到服务器的 cookie。它不会显示您在脚本生命周期中添加/更改/删除的任何 cookie。这些更改只会出现在 NEXT 请求中。
The only exception to this is $_SESSION
, which is populated when you call session_start()
.
唯一的例外是$_SESSION
,它在您调用时填充session_start()
。
If you need those values to be in $_COOKIE immediately, you'll have to add them manually, e.g.
如果您需要立即将这些值放在 $_COOKIE 中,则必须手动添加它们,例如
setcookie('cookie', $value, ....);
$_COOKIE['cookie'] = $value;
回答by JDot
According to the PHP Manual at http://php.net/manual/en/function.setcookie.php:
根据http://php.net/manual/en/function.setcookie.php上的 PHP 手册:
If output exists prior to calling this function, setcookie()will fail and return FALSE. If setcookie()successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.
如果在调用此函数之前存在输出,则setcookie()将失败并返回FALSE。如果setcookie()成功运行,它将返回 TRUE。这并不表示用户是否接受了 cookie。
In other words, the function setcookie()
is not working because it is inside the page. If you want it to work, you will need to put that function before the page, specifically before any headers.
换句话说,该函数setcookie()
不起作用,因为它在页面内。如果你想让它工作,你需要把这个函数放在页面之前,特别是在任何标题之前。
Do this:
做这个:
<?php
if ( isset($_POST['checkbox']) ) {
setcookie("cookie", "on", time()+3600*24);
echo "You checked the checkbox and a cookie was set with a value of:<br>";
} else {
setcookie("cookie", "off", time()+3600*24);
echo "You didn't check the checkbox and a cookie was set with a value of:<br>";
}
echo $_COOKIE['cookie'];
?>
<!doctype html>
<html>
<head>...</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label for="checkbox">Option 1:</label>
<input type="checkbox" name="checkbox" id="checkbox"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
回答by John Conde
Cookies don't kick in until after they are set and a new page request is sent. This is because cookies are sent with page requests, they just don't magically appear to a the server.
Cookie 直到它们被设置并发送新的页面请求后才会启动。这是因为 cookie 是随页面请求一起发送的,它们只是不会神奇地出现在服务器上。
Your solution is to do a page refresh after setting the cookie.
您的解决方案是在设置 cookie 后进行页面刷新。
// set cookie
setcookie("cookie", "off", time()+3600*24);
// not available because this cookie was not sent with the page request.
echo $_COOKIE['cookie'];