douqun1977 2013-02-24 13:48
浏览 111
已采纳

我的cookie已设置但php无法读取它

I've been trying to make a cookie autologin, but although the cookie is set, php can't read it. I know it is set because I can see it with Cookie Monster, I don't know what's wrong... Need help! It's driving me crazy!

The code to set the cookie (I do it before any HTML tag)

setcookie("autologin", $_SESSION['user'], time()+5184000, "/");

the code to retrieve it:

if (!isset($_SESSION['user']) && isset($_COOKIE['autologin'])) {
     $_SESSION['user']=$_COOKIE['autologin'];
}

UPDATE: I don't use the code above in the same script. I do login, close the browser, reopen it and try to get the cookie, cookie is in Cookie Monster but php can't see it.

FIX: My problem was I was trying to save a serialized object, $_SESSION['user'], in the cookie, it has been fixed with that:

setcookie("autologin", base64_encode($_SESSION['user']), time()+5184000, "/");

and retrieving with:

if (!isset($_SESSION['user']) && isset($_COOKIE['autologin'])) {
         $_SESSION['user']=base64_decode($_COOKIE['autologin']);
}
  • 写回答

2条回答 默认 最新

  • dsvyc66464 2013-05-14 20:00
    关注

    My problem was I was trying to save a serialized object, $_SESSION['user'], in the cookie, it has been fixed with that:

    setcookie("autologin", base64_encode($_SESSION['user']), time()+5184000, "/");
    

    and retrieving with:

    if (!isset($_SESSION['user']) && isset($_COOKIE['autologin'])) {
             $_SESSION['user']=base64_decode($_COOKIE['autologin']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?