douhan1860 2017-05-10 19:29
浏览 87
已采纳

为什么cookie过期?

On my website I set some cookies when users login so I can remember them so they don't have to login every time they re-visit the website.

I set the cookies in php using the following code:

setcookie("last_login", $loginID, time() + (86400 * 30), "/"); // 86400 = 1 day

And in my php.ini I even got this setting:

session.cookie_lifetime = 2592000

But even with all this when i close down the browser and re-open the website the cookies don't work in my php code and they dose not get logged in, but even more weird. Then when i try yo echo the cookie using:

echo $_COOKIE["last_login"];

It in the begining does not show up but sometimes after a few reloads it just randomly starts working with the cookie and it echo it out and you get logged in...?

So how can i make it so the cookies allways work within the next 30 days without it being weird?

  • 写回答

1条回答 默认 最新

  • dtm37893 2017-05-10 19:34
    关注

    Sometimes browsers clear cookies when closing them. A few things you can check just to be sure:

    • Is this happening in just one browser or all browsers you can get your hands on?
    • Is the time on your system correct?
    • Does setcookie return TRUE? Check it: var_dump(setcookie("last_login", $loginID, time() + (86400 * 30), "/"));
    • Is the correct php.ini loaded? Check phpinfo() to see if the settings you changed are visible or not.

    Edit:

    Responding to the follow up question about the domain (see comment): Yes you can.

    setcookie("last_login", $loginID, time() + (86400 * 30), "/", "domain.com");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机