简体   繁体   English

会话在php中结束时如何做

[英]How to do something when session ends in php

I have a question for a school project. 我有一个学校项目的问题。 How do I do an action when somebody's session gets ended. 当某人的会话结束时我该怎么做。 For instance, I want to delete a file that says a user is logged in when his session ends. 例如,我要删除一个文件,该文件说用户的会话结束时已登录。 If he closes the browser or if he doesn't do any request for a while? 如果他关闭浏览器,或者一段时间内没有任何请求?

There is no way to track this directly. 无法直接跟踪。 You could however use a database and store in it the last time the user loaded a page, then run a cron job all X minutes which checks which users haven't done anything in the last Y minutes. 但是,您可以使用数据库并在用户最后一次加载页面时将数据存储在数据库中,然后在所有X分钟内运行一次cron作业,以检查哪些用户在最近Y分钟内未执行任何操作。

You can do so by writing a custom session handler. 您可以通过编写自定义会话处理程序来实现。

On this site you'll find a proper explanation: 在此站点上,您会找到正确的解释:

http://phpmaster.com/writing-custom-session-handlers/ http://phpmaster.com/writing-custom-session-handlers/

However, be aware that 'garbage collection' (cleaning up the file as you want to do) does NOT take place directly after the session was closed! 但是,请注意,在会话关闭后,不会直接发生“垃圾回收”(按您的意愿清理文件)!

Php uses a setting 'session.gc_probability' that indicates that once every XXX requests, PHP will look for old/closed sessions and start to do garbage-collection Php使用设置“ session.gc_probability”,该设置指示每隔XXX个请求,PHP将查找旧的/关闭的会话并开始进行垃圾收集

http://www.php.net/manual/en/session.configuration.php#ini.session.gc-probability http://www.php.net/manual/en/session.configuration.php#ini.session.gc-probability

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

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