問題描述
我在同一頁面上設置 cookie 時遇到問題.我在我的網站上使用了 cookie,它運行良好,我傾向于將 php 設置在單獨的文件中.現(xiàn)在,我在同一頁面上設置了一個 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"; }
它總是說未設置.我嘗試在頁面加載時執(zhí)行此操作,但仍然無效.
It always says is not set. I tried doing this in page load but still doesn't work.
推薦答案
參見 setcookie() 手冊(強調我的):
設置 cookie 后,就可以在下一頁加載時使用 $_COOKIE 或 $HTTP_COOKIE_VARS 數(shù)組訪問它們.請注意,像 $_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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!