php setcookie无效

swordsharp1001 2009-05-26 06:20:59
本人新手,刚接触php 2~3天

setcookie以后取不到变量值
代码简化到test.php
<?php
setcookie("usrname","a fool");
?>

<?php
if(isset($_COOKIE["usrname"]){echo $_COOKIE["usrname"];}
else{ echo "big error";}
?>

仍然不能解决问题
添加路径以后也不能解决问题
$_COOKIE["usrname"]始终为空值,用$HTTP_COOKIE_VARS["usrname"]也不能取到
不知问题何在,翻了一些资料也不能找到解决方法
...全文
1101 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
askok 2009-06-06
  • 打赏
  • 举报
回复
setcookie前有输出吗?
php.net/setcookie
defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction).
tfxg 2009-06-06
  • 打赏
  • 举报
回复
如果問題是:
一個頁面設置cookie,到另外一個頁面使用的時候顯示不了值,那你必須得在頁面的最前加session_id(PHPSESSID);PHPSESSID是你設置session_id的名字,與你的配置文件php.ini設置的名字一樣,有些設成SID,那就寫SID
weitaonn 2009-06-06
  • 打赏
  • 举报
回复
$expire_day = 60;
$expire = time() + 60 * 60 * 24 * $expire_day;
setcookie("usrname","a fool",$expire,"/");

If set to '/', the cookie will be available within the entire domain.

21,890

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧