修改session.save_path(会话数据保存在服务器上的位置)后,session_edestroy不起作用



我在网站根上创建了一个文件夹,会话数据存储在public_html之外。我这样做是为了让我网站上的会话持续更长时间,因为我遇到了一个问题,它们会在30分钟后超时。我尝试了很多方法来修复它,但都没有成功,直到我尝试了下面的代码。我使用以下代码创建持续一天的会话,该代码解决了30分钟后超时的问题:

ini_set('session.save_path', '/home/server/.sessionsData');
ini_set('session.gc_maxlifetime', 86400); 
ini_set('session.cookie_lifetime', 86400);
ini_set('session.cache_expire', 86400);
ini_set('session.name', 'website');
session_start(); // Session ready to go!

做出这一更改后,会话在30分钟后不再超时,但我遇到了一个新问题,即我破坏会话的"注销代码"不再像以前那样结束会话。以下代码是我用来注销和销毁会话的代码,但它不再像以前一样工作:

session_start();
session_destroy();
header("location: https://website.com");

我应该怎么做才能销毁会话,并删除存储在"/home/server/.sessionsData"文件夹中的相应会话数据?如果我进入文件夹并直接删除会话数据文件,它将在用户浏览器中结束会话。

提前感谢您对此进行调查。

如注释中所述Perhaps your ini_set() code, or at least the path changing part, needs to be in your log out script as well.

代码形式:

ini_set('session.save_path', '/home/server/.sessionsData');
session_start();
session_destroy();
header("location: https://website.com");

session_unsetsession_destroy一起使用是实际清除SESSION数据的有效方法。

session_start();
session_unset(); //--> frees all session variables currently registered.
session_destroy(); //--> destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.

我还阅读了PHP手册上的评论,以下内容可以帮助顽固的浏览器:

session_write_close(); //--> End the current session and store session data.
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);  //--> replace the current session id with a new one, and keep the current session information.

最新更新