Jump to content

Self destructable session..


monkeytooth

Recommended Posts

Ok I want to create a session, and im sure theres a much better way for this (which if you know or have one im open to the idea) otherwise.. Im tempting to create a session that will destroy itself after x ammount of time... If it doesnt exisit I want it to create itself, and if its created I want it to reset it self while the user is on the site using the site or this part of it atleast. and by reset it I mean just set a new timestamp as the session while the user is using the site.. Now the kicker is after x ammount of time of being Idle I want the session to kill itself.. and all sessions currently active..

 

Below is my code.. its not working right somewhere I have created a loop like thing where i just bypasses the destroy and im fried from workin on this site im working on in general, so any help in figuring this out would be cool..

 

if ($_SESSION['last_seen'] == "") {
$_SESSION['last_seen'] = time();
} else {
$LIVEz = $_SESSION['last_seen'];
$DIEz = $_SESSION['last_seen'] - 5;
if ($LIVEz <= $DIEz) {
session_destroy();
} elseif ($LIVEz > $DIEz) {
$_SESSION['last_seen'] = time();
} else {
}
}

Link to comment
https://forums.phpfreaks.com/topic/125477-self-destructable-session/
Share on other sites

For the functionality that you describe you could use a database table to store your sessions along with timestamps.

You will need to add your own session functions and set your handlers with session_set_save_handler()

 

Include a cleanup routine that will destroy sessions past their max lifetime. You could set a lifetime for periods of inactivity i.e.

$sessionLife = 3440;

Lets assume I dont have SQL handy.. I do have it, but I dont want to use it for this..

 

Im trying to make a session that kills itself after 30 seconds.. 45 seconds.. an hour.. whatever.. something to be deemed at a later time as per the need.. So the assumption is if possible using time() that I can generate a timestamp and use that as the session data, in this case for sessions "last_seen". I have done similar to this in the past but for the life of me I cant get it to work now and I am at my wits end with this, but I need it..

 

that said this is my latest rendition of attempts.. still cant get it to work, can someone please help tell me where I am doing wrong with this.. or give me a better way please..

 

if (isset($_SESSION['last_seen'])) { 
echo "Session is: ". $_SESSION['last_seen'] . "<BR>";
$killurself = $_SESSION['last_seen'] + 5;

if ($_SESSION['last_seen'] <= $killurself) {
$_SESSION['last_seen'] = time();
$sessionstatus = "keep alive";
} else {
session_destroy();
$sessionstatus = "dead";
}

} else {
$_SESSION['last_seen'] = time();
$sessionstatus = "session made";
}
echo $sessionstatus;

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.