用户关闭浏览器时更新MySQL数据库 [英] Updating MySQL database when user closes browser

查看:144
本文介绍了用户关闭浏览器时更新MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一家研究公司开发Web应用程序,其中一项要求是用户一次只能登录一台设备.

I'm developing a web application for a research company and one of the requirements is that a user can only log in on one device at a time.

因此,我开发了一个非常基本的系统,当您登录一些PHP时,会将数据库中用户的行从0更新为1,并且当您单击注销按钮时,该行又从1更新为0.

So I developed a pretty basic system where when you log in some PHP updates a user's row in the database from 0 to 1 and when you click the logout button it updates that row again from 1 to 0.

唯一的问题是,如果用户不使用注销按钮而只是关闭浏览器,它将结束会话并将其注销,但数据库中的行将不会更新.

The only problem is if the user does not use the logout button and simply closes the browser, it will end the session and log them out but their row in the databse will not be updated.

有没有办法在浏览器关闭时更新数据库?

Is there a way to update the database on browser close?

推荐答案

您可以使用unload事件,在该事件中,您将向服务器发送ajax请求,该请求将更新mysql中的标志.您可以尝试遵循jquery语法.

You can use unload event where you will send an ajax request to server which will update the flag in mysql. You can try following jquery syntax.

$(window).bind('unload', function(){
    $.ajax({
        type: 'get',
        async: false,
        url: 'path/to/file.php'
    });
});

这篇关于用户关闭浏览器时更新MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆