javascript PHP自动注销,无需刷新页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28449124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:59:46  来源:igfitidea点击:

PHP automatic logout without having to refresh the page

javascriptphpjqueryhtmlajax

提问by JOB

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.

我正在用 PHP、JavaScript 和 HTML 编写一个项目。当用户空闲 1 分钟时,我已成功完成自动注销。但问题在于我必须刷新页面才能执行它并将我注销。

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?

有人可以帮助我,以便立即 1 分钟结束并且用户处于空闲状态,代码将被执行并且它将带我到登录页面而无需我刷新它?

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

回答by void

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

我想最好的实现方式是使用JS和PHP的组合

check.php

检查.php

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

else echo "1";
}

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

.js

.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 everyone minute.

或者只是将其包裹起来,setInterval(function(){},1*60*1000)而不是分钟setTimeout()检查一次

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

回答by mparafiniuk

You can't do that with php. You need to count time in javascript and make ajax request after that. Simplest way is probably use jQuery plugin like IdleTimeout.

你不能用 php 做到这一点。您需要在 javascript 中计算时间并在此之后发出 ajax 请求。最简单的方法可能是使用像 IdleTimeout 这样的 jQuery 插件。

回答by Angry 84

PHP code needs to be called/run to execute... It wont run in the background unless something is running it..

PHP 代码需要被调用/运行才能执行......除非有什么东西在运行它,否则它不会在后台运行......

You have a few options:

您有几个选择:

1: Ajaxas suggested by Shikhar.. You have to have something like a setTimeout in javascript which would call this every minute or how ever often

1:阿贾克斯由Shikhar的的建议..你必须有在javascript像一个的setTimeout这将调用这个每分钟通常怎么过

2: A cron job... but a cron job that checks every active session file and if it has expired, then you simply delete it (Kills a session)

2:一个cron 作业……但是一个 cron 作业会检查每个活动的会话文件,如果它已经过期,那么你只需删除它(杀死一个会话)

3: Have some other meansof loading a PHP file as often as you want to check

3:有一些其他的方式来加载一个 PHP 文件,只要你想检查

Even with the above, there is no sure way of doing this..

即使有了上述内容,也没有确定的方法可以做到这一点..

Cron job / PHP loading.. they need a trigger... and a cron job wont redirect your user the second their session is expired.

Cron 作业/PHP 加载......他们需要一个触发器......并且一个cron 作业不会在他们的会话过期后重定向你的用户。

Your only choice here is to use ajax on a timer to post to the sever and check if the session is still valid, if not then redirect once the response comes back from PHP to the ajax call.

您唯一的选择是在计时器上使用 ajax 发布到服务器并检查会话是否仍然有效,如果不是,则在响应从 PHP 返回到 ajax 调用后重定向。

But even then, once a user has logged in and if they disable JS... you wont get no redirect or checking.

但即便如此,一旦用户登录并且如果他们禁用了 JS ......你不会得到任何重定向或检查。

回答by dev_khan

@JOB You need to fire an ajax request something like following

@JOB你需要像下面这样触发一个ajax请求

    <script type="text/javascript">
         jQuery(document).ready(function(){
             setInterval(function(){
                 jQuery.ajax({
                     url: 'http://localhost/your_script.php',
                     type: 'POST',
                     success:function(response){
                         alert("You are logged out");
                         return false;
                     }
                 });
             }, 1000);
         });
    </script>

This script will check the status of logout after every 1 second and logs out the user if the user is idle for the time decided by PHP script.

该脚本每 1 秒检查一次注销状态,如果用户空闲时间由 PHP 脚本决定,则注销用户。