-
-
Save psdtohtml5/6090113 to your computer and use it in GitHub Desktop.
| <?php | |
| //on pageload | |
| session_start(); | |
| $idletime=60;//after 60 seconds the user gets logged out | |
| if (time()-$_SESSION['timestamp']>$idletime){ | |
| session_destroy(); | |
| session_unset(); | |
| }else{ | |
| $_SESSION['timestamp']=time(); | |
| } | |
| //on session creation | |
| $_SESSION['timestamp']=time(); |
Thanks Brother ...
superb bro
Nice code!. but when i implemented my website it's automatically logging out even while user is using the system..
I haven't implemented this in my project yet, but this looks perfect for what I was looking for, My suggestion is to redirect the user to the website home page after logging out
after these two lines
session_destroy();
session_unset();
add this line
header('Location: ../index.php');
thank you
It's really helpful.
Thanks! It helps me a lot.
WOW! it's really GREAT!!! Thank you
Question is there a way, force the page to refresh when the time out is reached?
like coding a refresh() method? or does it already exist in php?
If you can use JavaScript that is work.
Like this : setTimeout(function(){ location.reload(); }, 60005); // 60005 milliseconds
Can anyone help me how can I user above code in CodeIgniter?
Not working in codeigniter! "timestamp" is said to be an undefined index.
This is ok...but how to check wheather a user is doing activity...and after then only we will logout the user!!!
Thanks for the snippet! I modified it to work php7
** put this line in your login script - must have already done a session_start() **
$_SESSION['timestamp']=time();
** Load this on every page **
session_start();
$autologout=1800; //after 15 minutes of inactivity the user gets logged out
$lastactive = $_SESSION['timestamp'] ?? 0; // Use of 'Null Coalescing Operator' - pulls the timestamp or sets it to 0.
if (time()-$lastactive>$autologout){
$_SESSION = array(); // Clear the session data
setcookie(session_name(), false, time()-3600); // Clear the cookie
session_destroy(); // Destroy the session data
}else {
$_SESSION['timestamp']=time(); //Or reset the timestamp
}
Excelent brother!