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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭