在本地测试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, '/', $domain, false);
?>
至于chrome为什么不允许IP地址 这是一开始设计决定的:
https://code.google.com/p/chromium/issues/detail?id=56211