lalnfl Posted September 19, 2010 Share Posted September 19, 2010 How do you set a time limit on a session? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/ Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Share Posted September 19, 2010 Are you looking for an inactivity timer? session_start(); // 10 mins in seconds $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/213829-help-with-sessions/#findComment-1112872 Share on other sites More sharing options...
lalnfl Posted September 19, 2010 Author Share Posted September 19, 2010 Yes I am so that I can log the user out. Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112874 Share on other sites More sharing options...
lalnfl Posted September 19, 2010 Author Share Posted September 19, 2010 Could you explain the code for me? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112875 Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Share Posted September 19, 2010 Sure This starts the session: session_start(); This is just telling the script how long to let them be inactive(Must be in seconds): $inactive = 600; Setting the session_life variable: $session_life = Current Unix time: time() Subtracting the current inactive time(not completely sure): - $_session['timeout'] Saying if the session life time is greater than the $inactive time: if($session_life > $inactive) Do This: Destroy the session: { session_destroy() Redirect to the logout page: header("Location: logoutpage.php"); } If not greater than $inactive: Setting session timeout to the current Unix time: S_session['timeout']=time(); -edit- fixed minor mistype -edit- Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112877 Share on other sites More sharing options...
lalnfl Posted September 19, 2010 Author Share Posted September 19, 2010 Where does the $_Session['timeout'] come from? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112879 Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Share Posted September 19, 2010 Variables that start with an underscore _ are server variables that the server sets. Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112883 Share on other sites More sharing options...
PFMaBiSmAd Posted September 19, 2010 Share Posted September 19, 2010 Correct capitalization matters in programming, variable names are case-sensitive and session variables are $_SESSION Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112884 Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Share Posted September 19, 2010 Updated code: <?php session_start(); // 10 mins in seconds $inactive = 600; $session_life = time() - $_SESSION['timeout']; if($session_life > $inactive) { session_destroy(); header("Location: logoutpage.php"); } $_SESSION['timeout']=time(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112894 Share on other sites More sharing options...
lalnfl Posted September 19, 2010 Author Share Posted September 19, 2010 Okay it works and thanks for the help but I have one question. What does the $_SESSION['timeout']=time(); do? When its time() - $_SESSION['timeout'] isn't the time the same when you load the page? Or how does that work? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112946 Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Share Posted September 19, 2010 $_SESSION['timeout'] is a var that will be stored throughout your session on the site. $_SESSION['timeout']=time() sets this to the current time. time() - $_SESSION['timeout'] is subtracting timeout from current time. Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112961 Share on other sites More sharing options...
lalnfl Posted September 19, 2010 Author Share Posted September 19, 2010 I put it on all of my pages, but it doesn't work correctly. Is it not suppose to go on all pages? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112966 Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Share Posted September 19, 2010 Oops, try this one. I keep it in a file called timer.php and just include to keep it simple, just an Idea <?php $error = 'Your session timed out.'; session_start(); if($_SESSION['session_count'] == 0) { $_SESSION['session_count'] = 1; $_SESSION['session_start_time']=time(); } else { $_SESSION['session_count'] = $_SESSION['session_count'] + 1; } $session_timeout = 1800; $session_duration = time() - $_SESSION['session_start_time']; if ($session_duration > $session_timeout) { session_unset(); session_destroy(); session_start(); session_regenerate_id(true); setcookie("Key_WatsonN", 0, time()-3600); setcookie("Errors", 0, time()-3600); setcookie("UID", 0, time()-3600); setcookie("PWD", 0, time()-3600); setcookie(Errors, $error, time()+120000); $_SESSION["expired"] = "yes"; header("Location: /"); // Redirect to Login Page } else { $_SESSION['session_start_time']=time(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112973 Share on other sites More sharing options...
lalnfl Posted September 19, 2010 Author Share Posted September 19, 2010 Nice. Thank you very much for the help. Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112992 Share on other sites More sharing options...
WatsonN Posted September 19, 2010 Share Posted September 19, 2010 Sure thing Sorry if i confused you All you need to do is add your cookies that you need removed and change the timeout if you want too Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1112993 Share on other sites More sharing options...
lalnfl Posted September 20, 2010 Author Share Posted September 20, 2010 Okay I do have 1 more question, if I have a script that goes along with this one that logs the person in and out, but how about if the user closes the browser without either logging out or going over the inactivity time? How would you make it so it would show the person logged out? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113034 Share on other sites More sharing options...
WatsonN Posted September 20, 2010 Share Posted September 20, 2010 What you could do is when you set the cookie set it to expire when the session is closed Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113038 Share on other sites More sharing options...
lalnfl Posted September 20, 2010 Author Share Posted September 20, 2010 So I would set the cookie to 0 then? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113044 Share on other sites More sharing options...
WatsonN Posted September 20, 2010 Share Posted September 20, 2010 Yes or just omit that all together Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113047 Share on other sites More sharing options...
lalnfl Posted September 20, 2010 Author Share Posted September 20, 2010 Okay I am confused. Could you show an example of code that would do the log out thing? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113055 Share on other sites More sharing options...
WatsonN Posted September 20, 2010 Share Posted September 20, 2010 to make each one expire at the end of the session setcookie(name, value, 0) or setcookie(name, value) Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113059 Share on other sites More sharing options...
lalnfl Posted September 20, 2010 Author Share Posted September 20, 2010 When you log in would you set the cookie the same way? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113061 Share on other sites More sharing options...
WatsonN Posted September 20, 2010 Share Posted September 20, 2010 Sure would Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113062 Share on other sites More sharing options...
lalnfl Posted September 20, 2010 Author Share Posted September 20, 2010 It still shows that there is 1 person online. When I close the broswer and come back on the script where it changes the user to logged out, still has logged in. So I am stuck on how to figure this problem out? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113072 Share on other sites More sharing options...
WatsonN Posted September 20, 2010 Share Posted September 20, 2010 Can you post the script? Quote Link to comment https://forums.phpfreaks.com/topic/213829-help-with-sessions/#findComment-1113462 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.