問題描述
我無法使用以下行創建 Firefox cookie:
I am not able to create a Firefox cookie with following line:
setcookie("TestCookie", $value, time()+3600, "/", "localhost");
有人知道為什么嗎?
我已經檢查了 FF 中的設置,它接受來自第 3 方的 cookie,并在它們到期時被刪除.
I have checked the settings in FF and it accepts cookies from 3rd parties and are deleted when they expire.
我現在可以用這一行創建:
I can create now with this line:
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
setcookie('cookiename', 'data', time()+60*60*24*365, '/', $domain, false);
但是我如何刪除它?
我嘗試將 + 切換為 - 但沒有用.
I tried with just switching the + to - but it didn't work.
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
setcookie('cookiename', 'data', time()-60*60*24*365, '/', $domain, false);
推薦答案
我已經有一段時間沒有使用 localhost cookie 了,但是根據 PHP 手冊中的注釋,'localhost' 是域參數的無效值.
It's been awhile since I worked with localhost cookies, but according to the comments in the PHP manual, 'localhost' is an invalid value for the domain parameter.
要在本地主機上設置 cookie,請改用 false
.示例:
To set a cookie on localhost, use false
instead. Example:
setcookie("TestCookie", $value, time()+3600, "/", false);
參見http://www.php.net/manual/en/function.setcookie.php#73107
這篇關于為什么我不能在 Firefox 中創建 cookie?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!