会话超时或空闲时自动重定向用户 [英] Automatically re-direct a user when session Times out or goes idle

查看:327
本文介绍了会话超时或空闲时自动重定向用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个计时器在页面上每3分钟运行一次(javascript),以检测php会话($ _SESSION)是否超时......如果是,则自动重定向它们。



一个很好的例子,一个用户登录并运行楼梯,并且永远不会回来......我希望javascript通过一个简单的重定向来记录它们...... / p>

这可能吗?我怎么做这样的事情?我正在使用PHP和javascript。



编辑:
Rob在下面说的正是我正在寻找的......我安然无恙'quote \'...



我怀疑Mike要求的是当会话超时时,应该告诉浏览器导航离开当前页面。例如,一些银行在一段时间不活动后这样做。 - Rob Kennedy 5小时前

解决方案

您可以使用简单的元刷新

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

或者用PHP实现超时:

 在session_start(); 
if(isset($ _ SESSION ['LAST_REQUEST_TIME'])){
if(time() - $ _SESSION ['LAST_REQUEST_TIME']> 180){
//会话超时,上次请求超过3分钟前
$ _SESSION = array();
session_destroy();
}
}
$ _SESSION ['LAST_REQUEST_TIME'] = time();

如果会话仍然有效,则无需每隔3分钟检查一次。


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

解决方案

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.

这篇关于会话超时或空闲时自动重定向用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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