問(wèn)題描述
我在同一頁(yè)面上設(shè)置 cookie 時(shí)遇到問(wèn)題.我在我的網(wǎng)站上使用了 cookie,它運(yùn)行良好,我傾向于將 php 設(shè)置在單獨(dú)的文件中.現(xiàn)在,我在同一頁(yè)面上設(shè)置了一個(gè) 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);
然后檢查是否設(shè)置
if(isset($_COOKIE["rb_vote"])) {
echo "IS SET";}
else {
echo "IS NOT SET"; }
它總是說(shuō)未設(shè)置.我嘗試在頁(yè)面加載時(shí)執(zhí)行此操作,但仍然無(wú)效.
It always says is not set. I tried doing this in page load but still doesn't work.
推薦答案
參見(jiàn) setcookie() 手冊(cè)(強(qiáng)調(diào)我的):
設(shè)置 cookie 后,就可以在下一頁(yè)加載時(shí)使用 $_COOKIE 或 $HTTP_COOKIE_VARS 數(shù)組訪問(wèn)它們.請(qǐng)注意,像 $_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
這里是一個(gè)解決方法建議.這是不完美的,因?yàn)樗荒鼙WC cookie 確實(shí)得到設(shè)置,但在您的情況下可能會(huì)這樣做.
Here is a workaround suggestion. It's imperfect because it can't guarantee the cookie actually gets set, but might do in your case.
這篇關(guān)于無(wú)法在同一頁(yè)面上設(shè)置 PHP cookie的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!