duanqian9503 2017-02-15 13:00
浏览 138

用户应该在关闭浏览器选项卡后自动注销不仅浏览器

I am working on PHP and MySql. I need a feature, when user close browser tab, a user should log out automatically. I have used javascript onbeforeunload event. But it is not working, this event executes for page refresh also.

Here is my sample code,

window.onbeforeunload = function(){
    $.ajax({url: "logout.php", success: true});
    return 'Are you sure ?';
};
  • 写回答

1条回答 默认 最新

  • dongxi1680 2017-02-15 13:35
    关注

    Idea would to poke the backend every 2 seconds and expire the session when you stop for let's say 5 seconds? This way 3 to 5 seconds after closing the tab session would automatically expire.

    setInterval( function() {
      $.get('/poke.php');
    }, 2000); 
    

    While php side:

    // poke.php
    $_SESSION['poke'] = time();
    
    // Probably your index.php
    if (isset($_SESSION['poke'] && time() - $_SESSION['poke'] > 5) {
       // Do the logout
    } else {
      $_SESSION['poke'] = time();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记