简体   繁体   English

会话销毁后诱导功能

[英]Inducing a function after session is destroyed

I'd like to induce a function after the session is destroyed (ie user closes the browser). 我想在会话被破坏(即用户关闭浏览器)之后引入一个函数。 This function would change a value in my database from true to false. 此函数会将数据库中的值从true更改为false。 Is that possible? 那可能吗?

I'm guessing that you're wanting to figure out whether a user is online or not (or something similar?) The only way to "know" (read: guess) that a session is dead is to store the time that the user was last active and then compare that time against the current time. 我猜想您想弄清楚用户是否在线(或类似的东西?),“知道”(读:猜测)会话已死的唯一方法是存储用户的时间上次激活,然后将该时间与当前时间进行比较。 Then, depending on your needs, you can decide to make the assumption that a user who has been inactive for 30 minutes is offline. 然后,根据您的需求,您可以决定假设处于非活动状态30分钟的用户处于脱机状态。

Only way to do this is using Javascript to detect a close and using a Ajax request (using XMLHttpRequest for example) to your PHP page (which handles the logout part). 唯一的方法是使用Javascript检测关闭,并使用Ajax请求(例如使用XMLHttpRequest )到您的PHP页面(处理注销部分)。

See Browser/tab close detection using javascript (or any other language) for more information on the closing part. 有关结束部分的更多信息,请参见使用javascript(或任何其他语言)进行的浏览器/标签关闭检测

I would suggest you to register the user last activity in your database, this way it's 100% accurate. 我建议您在数据库中注册用户的上一次活动,这样可以100%准确。 Then by comparing the registered date with the current time, you can decide when the user is active or not. 然后,通过将注册日期与当前时间进行比较,可以确定用户何时处于活动状态。

If you really want to know if the user is still on your page, you could also do something, but you should avoid it : On the client side, make a javascript loop, for example each minute, that tell to your server the client is still there. 如果您真的想知道用户是否仍在您的页面上,也可以做一些事情,但是您应该避免这样做:在客户端,例如,每隔一分钟进行一次javascript循环,以告知服务器客户端还在那儿。 When the client will close his page, the loop won't make any new query. 当客户端关闭页面时,循环将不会进行任何新查询。

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

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