无法在同一页面上设置 PHP cookie
我在同一页面上设置 cookie 时遇到问题.我在我的网站上使用了 cookie,它运行良好,我倾向于将 php 设置在单独的文件中.现在,我在同一页面上设置了一个 cookie,但它似乎不起作用.
I'm having trouble setting cookies on the same page. I used cookies on my site and it works fine, I tend to set make the php in separate file. Now, I'm setting a cookie on the same page but it doesn't seem to work.
$expire = time()+5;
setcookie("rb_vote", 1, $expire);
然后检查是否设置
if(isset($_COOKIE["rb_vote"])) {
echo "IS SET";}
else {
echo "IS NOT SET"; }
它总是说未设置.我尝试在页面加载时执行此操作,但仍然无效.
It always says is not set. I tried doing this in page load but still doesn't work.
推荐答案
参见 setcookie() 手册(强调我的):
设置 cookie 后,就可以在下一页加载时使用 $_COOKIE 或 $HTTP_COOKIE_VARS 数组访问它们.请注意,像 $_COOKIE 这样的超全局变量在 PHP 4.1.0 中可用.Cookie 值也存在于 $_REQUEST
Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_REQUEST
这里是一个解决方法建议.这是不完美的,因为它不能保证 cookie 确实得到设置,但在您的情况下可能会这样做.
Here is a workaround suggestion. It's imperfect because it can't guarantee the cookie actually gets set, but might do in your case.
相关文章