dongou6632 2009-06-16 17:58
浏览 57
已采纳

会话超时或空闲时自动重定向用户

I want to have a timer going to run every 3 minutes on the page (javascript), to detect if a php session ($_SESSION) has timed out... and if so, redirect them automatically.

A good example would be, a user logs in and runs up stairs, and never comes back down... I want the javascript to log them out with a simple redirect...

Is this possible? and how would I do such a thing? I am using PHP and javascript.

Edit: What Rob said below is exactly what I am looking for... and I safely \'quote \'...

I suspect what Mike is asking for is that when the session times out, the browser should be told to navigate away from the current page. Some banks do this after a period of inactivity, for example. – Rob Kennedy 5 hours ago

  • 写回答

3条回答 默认 最新

  • douwuli4512 2009-06-16 18:00
    关注

    You could use a simple meta refresh:

    <meta http-equiv="refresh" content="180;url=http://example.com/logout" />
    

    Or you implement a timeout with PHP:

    session_start();
    if (isset($_SESSION['LAST_REQUEST_TIME'])) {
        if (time() - $_SESSION['LAST_REQUEST_TIME'] > 180) {
            // session timed out, last request is longer than 3 minutes ago
            $_SESSION = array();
            session_destroy();
        }
    }
    $_SESSION['LAST_REQUEST_TIME'] = time();
    

    Then you don’t need to check every 3 minutes if the session is still valid.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi