简体   繁体   English

如何让用户使用 PHP Sessions 登录 1 年?

[英]How to keep a user logged in with PHP Sessions for 1 year?

I'm currently using the following code:我目前正在使用以下代码:

ini_set('session.gc_maxlifetime', 31968000);
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 1);
ini_set('session.cookie_lifetime', 31968000);

if(isset($_COOKIE[session_name()])) {
    setcookie(session_name(), $_COOKIE[session_name()], time() + 31968000, "/", ".********.net");
}

I'm using that code for the "landing page" after someone logged in or registered successfully (after registration you'll be automatically logged in and transferred to the "landing page")有人登录或注册成功后,我将该代码用于“登陆页面”(注册后,您将自动登录并转移到“登陆页面”)

For some reason this works for a couple of minutes.出于某种原因,这会持续几分钟。 I can close the App Safari and open it again for a few seconds and then maybe after half an hour it stops working.我可以关闭 App Safari 并再次打开它几秒钟,然后可能在半小时后它停止工作。 First I thought it would be working.首先我认为它会起作用。

Any ideas?有任何想法吗?

Btw, 31968000 is more than one year of time in seconds.顺便说一句,31968000 是一年多的时间(以秒为单位)。

I had to change the properties session.gc_maxlifetime, session.gc_probability, session.gc_divisor and session.cookie_lifetime in the php.ini file in order to make it work.我必须更改 php.ini 文件中的 session.gc_maxlifetime、session.gc_probability、session.gc_divisor 和 session.cookie_lifetime 属性才能使其工作。

Also it's important to restart the Apache server and php-fpm to load the edited php.ini file.此外,重新启动 Apache 服务器和 php-fpm 以加载编辑过的 php.ini 文件也很重要。

service apache2 restart

service php7.3-fpm restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM