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'); } 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(); ?> 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? 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 Quote 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?"... 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. 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 Quote 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.. 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. 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
Archived
This topic is now archived and is closed to further replies.