monkeytooth Posted September 23, 2008 Share Posted September 23, 2008 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 { } } Quote Link to comment https://forums.phpfreaks.com/topic/125477-self-destructable-session/ Share on other sites More sharing options...
trq Posted September 23, 2008 Share Posted September 23, 2008 The behavour you describe is how sessions work by default. Quote Link to comment https://forums.phpfreaks.com/topic/125477-self-destructable-session/#findComment-648708 Share on other sites More sharing options...
monkeytooth Posted September 23, 2008 Author Share Posted September 23, 2008 whats there defualt time out though?.. i know they die when you close the browser.. but im trying to give it a specific expiration.. and upon expiring destroy all sessions.. Quote Link to comment https://forums.phpfreaks.com/topic/125477-self-destructable-session/#findComment-648719 Share on other sites More sharing options...
JonnoTheDev Posted September 23, 2008 Share Posted September 23, 2008 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; Quote Link to comment https://forums.phpfreaks.com/topic/125477-self-destructable-session/#findComment-648737 Share on other sites More sharing options...
monkeytooth Posted September 23, 2008 Author Share Posted September 23, 2008 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; Quote Link to comment https://forums.phpfreaks.com/topic/125477-self-destructable-session/#findComment-648791 Share on other sites More sharing options...
CroNiX Posted September 23, 2008 Share Posted September 23, 2008 search php.ini for 'session.gc_maxlifetime' Quote Link to comment https://forums.phpfreaks.com/topic/125477-self-destructable-session/#findComment-649088 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.