简体   繁体   English

用户不活动注销PHP

[英]User Inactivity Logout PHP

I want my users to be logged out automatically after X minutes of inactivity. 我希望我的用户在X分钟不活动后自动注销。 I also want to have all sessions destroyed. 我也想要破坏所有会话。

How can this be done? 如何才能做到这一点? How can I check for inactivity then perform a function to log them out??? 如何检查不活动然后执行一个函数来记录它们?

I tired Michiels approach and got no where. 我厌倦了Michiels的方法并且没有在哪里。 On investigation I saw that the if statement simply added the expiry period to the current time so the statement never fired. 在调查中,我看到if语句只是将到期时间段添加到当前时间,因此该语句从未触发。

This is my altered version: 这是我的修改版本:

set this when logging in user or loading a secure page: 登录用户或加载安全页面时设置此项:

 $_SESSION['expire'] = time()+1*60;

And use this to see if the expiry time is less than current time (ie we're past the expiry limit): 并使用它来查看到期时间是否小于当前时间(即我们已超过到期限制):

if(time() > $_SESSION['expire']){
 $user -> logout();
}

You can set session time out limit like: 您可以设置会话超时限制,如:

ini_set('session.gc_maxlifetime',30);

Here is the possible solution for you. 以下是适合您的解决方案。

You could also do: 你也可以这样做:

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

On every page, and when the user is trying to navigate and he has been inactive for an twenty minutes you can log him out like this: 在每个页面上,当用户尝试导航并且他已经处于非活动状态20分钟时,您可以将其记录下来,如下所示:

if($_SESSION['loginTime'] < time()+20*60){ logout(); }

Depending on how fast your server is and how many users you have, you can have it send a request to your server whenever a user does anything (navigates, clicks a button, whatever). 根据服务器的速度和用户的数量,您可以在用户执行任何操作时向服务器发送请求(导航,单击按钮等)。 From this request, update a SQL table with their last activity time. 从此请求中,使用上次活动时间更新SQL表。

Have a cron job run through the table at some regular interval and delete the sessions of the users that have been inactive for whatever your threshold is going to be. 让cron作业以一定的时间间隔在表中运行,并删除对于您的阈值将处于非活动状态的用户的会话。

If your server is slow or you have a lot of users, you can have this script run infrequently. 如果您的服务器速度很慢或者您拥有大量用户,则可以不经常运行此脚本。

PHP's session mechanism already have a garbage collector based on the inactivity timeout. PHP的会话机制已经有一个基于不活动超时的垃圾收集器。 You have no worry about. 你不用担心。

You can set the last active time by $_SESSION['lastactive'] = time() and update it every time when user navigates to a new page. 您可以通过$ _SESSION ['lastactive'] = time()设置上次活动时间,并在每次用户导航到新页面时更新它。 Then you can have a function timeout() on every page . 然后你可以在每个页面上有一个函数timeout()。

function timeout()    
{
    $maxtime = 60*2; // Here , maxtime has been set to 2 minutes

if(isset($_SESSION['lastactive']) and (time() - $_SESSION['lastactive'] > $maxtime )) // subtracting current time from lastactive time and seeing if it exceeded timeout limit.
{
    signout(); //logging out        
}

if(isset($_SESSION['lastactive']) and (time() - $_SESSION['lastactive'] < $maxtime )) // subtracting current time from lastactive time and seeing if it exceeded timeout limit.
{   
    return 1; // timeout limit not exceeded     
}   
else
{
    if(!isset($_SESSION['lastactive']))
    {

        $_SESSION['lastactive'] = time(); //if lastactive is not set
    }
}
}


$(document).ready( function() $(document).ready(function()
{ {
setTimeout(function() { CALL LOGOUT.PHP VIA AJAX }, 720000); setTimeout(function(){ CALL LOGOUT.PHP VIA AJAX },720000);

}); });

720000 means 12 minutes ( for illustration purpose ) 720000表示12分钟 (仅供参考)
put this script in your header and set ur own time of inactivity 将此脚本放在标题中,并设置自己的不活动时间
you can set what time u want , it will be work like if you set 5 minutes then when u login to system then it start count for 5 min. 你可以设置你想要的时间,就像你设置5分钟然后当你登录系统然后它开始计数5分钟一样。 but if u click on any module this script will be reloaded , because when page turns then header is also reload when script is reload then it start count from 0 (initial), but if u cant access the system within 5 min. 但如果你点击任何模块,这个脚本将被重新加载,因为当页面转动时,当重新加载脚本时头文件也会重新加载,然后它从0开始计数(初始),但如果你不能在5分钟内访问系统。 then it will load the logout.php and system will logs-out 然后它将加载logout.php并且系统将注销

Use unset($_SESSION['NAME']); 使用未unset($_SESSION['NAME']); or session_destroy(); session_destroy(); . You could also change the value of the session. 您还可以更改会话的值。

To do this at a certain time, you would need to set a timestamp in the database, and then call it to check if it's beyond X minutes. 要在特定时间执行此操作,您需要在数据库中设置时间戳,然后调用它以检查它是否超过X分钟。 Look at the link at the bottom. 看看底部的链接。

I'd personally just use cookies and make them expire at a certain time, but whatever floats your boat. 我个人只是使​​用cookies并让它们在某个时间到期,但无论你的船是什么漂浮。

If current time is more than 30 seconds past time X (from the database) 如果当前时间超过30秒(从数据库)

this is how i do it : 我就是这样做的:

//set timeout period in seconds
$idleTime= 60*2;
//check to see if $_SESSION['timeout'] is set
if(isset($_SESSION['timeout'])){
$session_life = time() - $_SESSION['timeout'];
if($session_life > $idleTime){
// your logout code here*
     }
}
$_SESSION['timeout'] = time();

This makes $_SESSION['timeout'] reset every time a page is reloaded, i have this in an include file in the header of every sub page, works for me atleast. 这使得每次重新加载页面时都会重置$ _SESSION ['timeout'],我在每个子页面的标题中都有一个包含文件,至少对我有效。

The simplest way is this. 最简单的方法就是这样。 Send the user to a log out page if they are not activating certain elements on your website 如果用户未激活您网站上的某些元素,请将用户发送到注销页面

$secondsWait = 300; // these are seconds so it is 300s=5minutes
header("refresh:$secondsWait; logout.php");

contents for the redirect... logout.php, destroy any sessions and maybe also send a message alerting the user why they were logged out redirect ... logout.php的内容,销毁任何会话,也可能发送一条消息,提醒用户注销的原因

<?php
session_start();
session_unset();
session_destroy();  
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM