Reputation: 4809
I have the following code on my website:
session_set_cookie_params(7200, '.website.com', '', false, true);
If I browse my website, sometimes I will be logged out of my website after the two hours. For example if I am submitting a form, and that's very frustrating.
My question is: how could I solve this problem? Is it possible to logout the user only when he is INACTIVE for 2 hours? And what should be the first timeout value of session_set_cookie_params
?
Upvotes: 0
Views: 597
Reputation: 1323
You need to save into SESSION an actual time of the last visitor move on your website.
Login function:
login_user();
save_actual_time_in_session();
when user is logged in, for every page load do:
check_if_time_in_session_is_expired(); // if so, do some action, e.g. redirect to login form
save_actual_time_in_session();
(@Mattios550 solution won't work because on every HTTP request is loginTime
value in session refreshed to actual time so it never expires...)
Upvotes: 0
Reputation: 372
Try this:
$_SESSION['loginTime'] = time();
if($_SESSION['loginTime'] < time()+120*60){ logoutfunction(); }
The 120
part is the 120 minutes you specified (2 hours). You You could be really clever and ask the user how long they want to wait before they're logged out :D
You'd also need to replace the logoutfunction();
with whatever you want to happen if the user should be logged out (probably use session_destroy(); header("Location: http://yourwebsite.com/login.php");
)
Please let me know if you need any more help with this :)
Upvotes: 1