chrome 下 php setcookie domain设置为localhost 或 127.0.0.1 取不到值

来源:互联网 发布:js 设置select 选中值 编辑:程序博客网 时间:2024/05/17 00:49

在本地测试setcookie  

$cookiename = 'a';$cookievalue = 'aa';setcookie($cookiename,$cookievalue,time()+60*24,'/','127.0.0.1');echo $_COOKIE['a'];

在php官网上有这样的描述:

something that wasn't made clear to me here and totally confused me for a while was that domain names must contain at least two dots (.), hence 'localhost' is invalid and the browser will refuse to set the cookie! instead for localhost you should use false.

to make your code work on both localhost and a proper domain, you can do this:

<?php

$domain 
= ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
setcookie('cookiename''data'time()+60*60*24*365'/'$domainfalse);

?>


至于chrome为什么不允许IP地址  这是一开始设计决定的:

https://code.google.com/p/chromium/issues/detail?id=56211


原创粉丝点击