Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP : Auto expire session / Auto logout after specific time
<?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();
@rakeshkumar125

This comment has been minimized.

Copy link

rakeshkumar125 commented Nov 23, 2015

Nice code working in good way

@snkhan120

This comment has been minimized.

Copy link

snkhan120 commented Dec 15, 2015

Perfect example
Thanks Man

@alejandrovalencias

This comment has been minimized.

Copy link

alejandrovalencias commented Mar 4, 2016

Excelent brother!

@infohtreasure

This comment has been minimized.

Copy link

infohtreasure commented Jul 13, 2016

Thanks Brother ...

@h9ri

This comment has been minimized.

Copy link

h9ri commented Jun 24, 2017

superb bro

@thanooshan

This comment has been minimized.

Copy link

thanooshan commented Oct 6, 2017

Nice code!. but when i implemented my website it's automatically logging out even while user is using the system..

@dextel2

This comment has been minimized.

Copy link

dextel2 commented Nov 27, 2017

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

@sabbir420

This comment has been minimized.

Copy link

sabbir420 commented Nov 28, 2017

It's really helpful.

@rjtamayo

This comment has been minimized.

Copy link

rjtamayo commented Jan 16, 2018

Thanks! It helps me a lot.

@scarleaf

This comment has been minimized.

Copy link

scarleaf commented Feb 6, 2018

WOW! it's really GREAT!!! Thank you

@TheYoungerSes

This comment has been minimized.

Copy link

TheYoungerSes commented Jun 1, 2018

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?

@paultapas30

This comment has been minimized.

Copy link

paultapas30 commented Jun 18, 2018

If you can use JavaScript that is work.
Like this : setTimeout(function(){ location.reload(); }, 60005); // 60005 milliseconds

@Naren-hybreeder

This comment has been minimized.

Copy link

Naren-hybreeder commented Sep 25, 2018

Can anyone help me how can I user above code in CodeIgniter?

@dreamerdan54

This comment has been minimized.

Copy link

dreamerdan54 commented Jan 16, 2019

Not working in codeigniter! "timestamp" is said to be an undefined index.

@Tanmoy5992

This comment has been minimized.

Copy link

Tanmoy5992 commented Feb 15, 2019

This is ok...but how to check wheather a user is doing activity...and after then only we will logout the user!!!

@mwoare

This comment has been minimized.

Copy link

mwoare commented Mar 13, 2019

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
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.