Jump to content

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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.