PHP自动注销,而无需刷新页面 [英] PHP automatic logout without having to refresh the page

查看:267
本文介绍了PHP自动注销,而无需刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在PHP,JavaScript和HTML的项目。我已经成功地完成了自动注销,当用户空闲1分钟。但问题来的,我需要刷新页面,它要执行和记录了我。

有人可以帮我,让马上为1分钟以上,用户空闲时,code将被执行,它会带我到登录页面,没有我刷新呢?

下面是我的code:

  //检查,看看是否$ _SESSION ['超时']设置
如果(使用isset($ _ SESSION ['超时'])){
$ session_life =时间() -  $ _SESSION ['超时'];
如果($ session_life> $无效)
    {


 回声<脚本类型=文/ JavaScript的'>
window.alert('您的会议得到了过期');
&所述; /脚本>中;
标题(位置:logout.php);
}
}

$ _SESSION ['超时'] =时间();
//延续等codeS
 

解决方案

我想实现是通过使用JS和PHP相结合的最佳方式

check.php

 如果(使用isset($ _ SESSION ['超时'])){
$ session_life =时间() -  $ _SESSION ['超时'];
如果($ session_life> $无效)回声0;

其他回声1;
}

$ _SESSION ['超时'] =时间();
 

的.js

  $(文件)。就绪(函数(){
  的setTimeout(函数(){
        $获得(check.php功能(数据){
        如果(数据== 0)window.location.href =logout.php;
        });
    },1 * 60 * 1000);
});
 

或者只是把它包在的setInterval(函数(){},1 * 60 * 1000)而不是的setTimeout()如果你想它之后的每次一分钟进行检查。

  $(文件)。就绪(函数(){
的setInterval(函数(){
        $获得(check.php功能(数据){
        如果(数据== 0)window.location.href =logout.php;
        });
    },1 * 60 * 1000);
});
 

I am writing a project in PHP, JavaScript and HTML. I have successfully done the automatic logout when the user is idle for 1 minute. But the problem comes in that I have to refresh the page for it to be executed and log me out.

Can somebody help me so that immediately 1 minute is over and the user is idle, the code will be executed and it will take me to the login page without me refreshing it?

Here is my code:

// check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['timeout'];
if($session_life > $inactive)
    {  


 echo"<script type='text/javascript'> 
window.alert('Your Session got Expired');
</script>";
header("Location: logout.php");
}
}

$_SESSION['timeout'] = time();
//Continuation of other codes

解决方案

I guess the best way to implement is by using the combination of JS and PHP

check.php

if(isset($_SESSION['timeout']) ) {
$session_life = time() - $_SESSION['timeout'];
if($session_life > $inactive) echo "0";

else echo "1";
}

$_SESSION['timeout'] = time();

.js

$(document).ready(function(){
  setTimeout(function(){
        $.get("check.php", function(data){
        if(data==0) window.location.href="logout.php";
        });
    },1*60*1000);
});

Or just wrap it in setInterval(function(){},1*60*1000) instead of setTimeout() if you want it to be checked after every one minute.

$(document).ready(function(){
setInterval(function(){
        $.get("check.php", function(data){
        if(data==0) window.location.href="logout.php";
        });
    },1*60*1000);
});

这篇关于PHP自动注销,而无需刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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