我们可以在 php 中创建无休止的会话吗?

问题描述:

通常,当用户关闭浏览器时,php 会话就会过期.是否可以在 php 中创建一个永不过期的会话,不管用户关闭和重新启动浏览器多少次?

Normally a php session expires when the user closes the browser. Is is possible to create a session in php that never expires, doesn't matter how many times user closes and restart the browser?

谢谢

严格来说不是无限的,但是你可以将 cookie 的生命周期设置为两年左右,这非常接近:

Not strictly endless, but you can set the cookie lifetime to two years or so which comes pretty close:

  • session.cookie-lifetime ini setting
  • session_set_cookie_params()Docs function to set them programmatically.

如果用户关闭浏览器,会话 cookie 不会被删除.

The session cookie won't be deleted then if the user closes the browser.

注意您的会话数据存储 保持数据也那么长.这个很重要.请记住,您需要存储这么长的时间跨度内的所有用户数据,因此您应该有足够的可用空间.

Take care that your session data store keeps the data as well that long. This is important. And keep in mind that you need to store all user's data for this large time-span, so you should have enough space available.

如果用户在她或他的浏览器中禁用了 cookie,这根本不起作用.

This does not work at all if the user disables cookies in her or his browser.