Jump to content

Automatically logout after 15 mins via sessions


playaz

Recommended Posts

Hey there...

 

Here's something I came up with that'll work just fine.

The script in its current state isn't fort knox (actually, you can beat the login again by clearing your browser cookies).

 

I did make this script and usaully what happens in these forums is we only hint at you to find the answer yourself.

I actually solved the problem for you, and I don't mind, but please do me and the other forum posters one thing.

 

***** Learn From The Script*****

 

Do not just implement it and never give it a second thought.

Anyway, here it is, functional in full...And well documented!

 

<?php

// Start or Resume a Session
session_start();

// How long is the user allowed to be idle?
$idle = (15 * 60); // In Seconds

// See if the User has been idle for too long
// If he/she has, then log them out.
// Otherwise, update their status.
if(isset($_SESSION['time'])){

// Debug
debug($idle);

// How long from his/her last visit?
$time_elapsed = (time() - $_SESSION['time']);

if($time_elapsed < $idle){
	// Update user's activity
	$_SESSION['time'] = time();
	echo 'Activity Updated';
}else{
	// He/She has been idle for too long
	echo 'You have been idle for too long<br />Please login again';

	// Do Your Logout Function Here
}
}else{
// This is his/her first visit.
$_SESSION['time'] = time();
echo 'First Visit';

// Possibly Require Login Here
}

function debug($idle){
echo '<br />------- Debug -------';
echo '<br />Time: '.time();
echo '<br />Last Activity: '.$_SESSION['time'];
echo '<br /> Time Elapsed: '.(time() - $_SESSION['time']);
echo '<br />Idle Time: '.$idle;
echo '<br />----- End Debug -----<br /><br />';
}

?>

Cheers rlelek.. will have another look at that 2mrw when I get some free time - much appreciated :)

 

P.S I always learn from you forum guys hence the reason I'm here lol :-)

 

Thanks again

 

P.P.S isedeasy, your input also is appreciated too :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.