rahulvicky00 Posted December 15, 2011 Share Posted December 15, 2011 As my title said the question, i want to destroy my session after a particular time if user is being ideal.. currently i am using this code to define my session: session_start(); // Check, if username session is NOT set then this page will jump to login page if (!isset($_SESSION['myusername'])) { header('Location: admin.php'); } Quote Link to comment https://forums.phpfreaks.com/topic/253235-how-to-destroy-session-automatically-after-defined-time/ Share on other sites More sharing options...
kney Posted December 15, 2011 Share Posted December 15, 2011 <?php session_start(); // timeout after 10 minutes $inactive = 600; $session_life = time() - $_session['timeout']; if($session_life > $inactive) { session_destroy(); header("Location: logoutpage.php"); } S_session['timeout']=time(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/253235-how-to-destroy-session-automatically-after-defined-time/#findComment-1298147 Share on other sites More sharing options...
ManiacDan Posted December 15, 2011 Share Posted December 15, 2011 You need to be more specific, do you want: 1) The session to expire after a certain amount of inactivity? For instance, if they get up and go to lunch, when they come back they'll be logged out? 2) The session to expire at a specific time after login regardless of their activity. For instance, they can only browse your site for 4 hours before they're automatically kicked out? Quote Link to comment https://forums.phpfreaks.com/topic/253235-how-to-destroy-session-automatically-after-defined-time/#findComment-1298149 Share on other sites More sharing options...
rahulvicky00 Posted December 15, 2011 Author Share Posted December 15, 2011 You need to be more specific, do you want: 1) The session to expire after a certain amount of inactivity? For instance, if they get up and go to lunch, when they come back they'll be logged out? 2) The session to expire at a specific time after login regardless of their activity. For instance, they can only browse your site for 4 hours before they're automatically kicked out? I need something like "The session to expire after a certain amount of inactivity? For instance, if they get up and go to lunch, when they come back they'll be logged out?"... Quote Link to comment https://forums.phpfreaks.com/topic/253235-how-to-destroy-session-automatically-after-defined-time/#findComment-1298181 Share on other sites More sharing options...
ManiacDan Posted December 15, 2011 Share Posted December 15, 2011 session_set_cookie_params allows you to set the timeout for the user's session cookie. kney's solution accomplishes roughly the same thing at the server level, but note that you need to die() after a header() call. Quote Link to comment https://forums.phpfreaks.com/topic/253235-how-to-destroy-session-automatically-after-defined-time/#findComment-1298186 Share on other sites More sharing options...
rahulvicky00 Posted December 15, 2011 Author Share Posted December 15, 2011 session_set_cookie_params allows you to set the timeout for the user's session cookie. kney's solution accomplishes roughly the same thing at the server level, but note that you need to die() after a header() call. Thanks for a very quick reply but can you give me some coding, actually i am very new in PHP... A example code would be a grate help.. Quote Link to comment https://forums.phpfreaks.com/topic/253235-how-to-destroy-session-automatically-after-defined-time/#findComment-1298194 Share on other sites More sharing options...
ManiacDan Posted December 15, 2011 Share Posted December 15, 2011 that function is one line. Put that one line above session_start. The first argument is the number of seconds of inactivity. Quote Link to comment https://forums.phpfreaks.com/topic/253235-how-to-destroy-session-automatically-after-defined-time/#findComment-1298197 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.