无法在同一网页上设置PHP Cookie [英] Can't set PHP cookie on the same page

查看:151
本文介绍了无法在同一网页上设置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或$ HTTP_COOKIE_VARS数组访问在下一页加载。注意,superglobals如$ _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.

这篇关于无法在同一网页上设置PHP Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆