您的位置:首页 > 其它

setcookie使用localhost不能成功的问题

2010-08-22 12:45 477 查看
问题:

setcookie("severss", "ss",$num, '/','localhost');
SetCookie("sever","dd",$num,"/","127.0.0.1")
两个setcookie都成功!
但是用localhost的那个在localhost域名下就不可以访问
而127.0.0.1在127.0.0.1下能访问
这是为什么阿?
要怎么写才能在localhost下设置cookie成功呢?
怎样在http://localhost/kongjian/admin.php/account/login下设置cookie然后在http://localhost/kongjian/下边也可以访问到?

手册上这么说:

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);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: