PHP session destroyed too soon on mobile browser(s)

落花浮王杯 提交于 2019-12-23 18:15:04

问题


I have the following issue. I am building a PHP + jQuery mobile website and I want to maintain the PHP session. The problem here is that when I close the browser App (Samsung Galaxy S4/S5 or Google Chrome on Android) and I leave the phone for say, ten minutes, and then reopen the browser, the whole session appears to be destroyed and I have to log in again.

I tried to increase the cookie lifetime like this:

ini_set('session.cookie_lifetime', 60 * 60 * 24);
ini_set('session.gc-maxlifetime', 60 * 60 * 24);
session_start();

But this does not solve it. I think it is because the browser app purges the session data after some time of inactivity.

Note that I do not want to use "remember me" functionality, I just want to use the PHP session ID.

What would be the correct way to solve this? I have looked at local storage but it seems strange the browser App purges the entire session and I am looking for the simplest solution.


回答1:


Instead of trying to modify the ini values try using the session_set_cookie_params function like so:

session_set_cookie_params(60 * 60 * 24);
session_start();



回答2:


I fixed it (after a lot of trying out) using the php.ini setting

session.gc_maxlifetime = 3600

After doing this, the mobile browser kept the session alive. Even if I put my phone away for a longer period.

Apparently setting the maxlifetime does not work if done through a script.



来源:https://stackoverflow.com/questions/32111086/php-session-destroyed-too-soon-on-mobile-browsers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!