我正在研究PHP和MySql。我需要一个功能,当用户关闭浏览器选项卡时,用户应该自动注销。我使用过javascript onbeforeunload event。但它不起作用,此事件也会执行页面刷新。
下面是我的示例代码,
window.onbeforeunload = function(){
$.ajax({url: "logout.php", success: true});
return 'Are you sure ?';
};发布于 2017-02-15 21:35:33
我的想法是每隔2秒戳一次后端,当你停止5秒的时候终止会话?这样,在关闭选项卡会话后3到5秒将自动过期。
setInterval( function() {
$.get('/poke.php');
}, 2000); 而php端:
// poke.php
$_SESSION['poke'] = time();
// Probably your index.php
if (isset($_SESSION['poke'] && time() - $_SESSION['poke'] > 5) {
// Do the logout
} else {
$_SESSION['poke'] = time();
}https://stackoverflow.com/questions/42250147
复制相似问题