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

为什么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?

图片转代码服务由CSDN问答提供 功能建议

在我的网站上,我在用户登录时设置了一些cookie,这样我就能记住它们,这样他们就不必每次都登录 他们重新访问网站的时间。

我使用以下代码在php中设置了cookie:

  setcookie(“last_login”,  $ loginID,time()+(86400 * 30),“/”);  // 86400 = 1天
   
 
 

在我的php.ini中我甚至得到了这个设置:

session.cookie_lifetime = 2592000

但即便如此,当我关闭浏览器并重新打开网站时,我的PHP代码中的cookie无法正常工作,并且它们无法登录,但更多 奇怪的。 然后当我尝试使用以下方式回复cookie:

  echo $ _COOKIE [“last_login”]; 
   
 
 

它在开始时没有显示,但有时在几次重新加载之后它只是随机开始使用cookie并且它回显它并且你登录了...?

那怎么能 我是这样做的,所以饼干总是在接下来的30天内工作,而不是很奇怪?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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");
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题