I'd like to know how to keep the user logged in indefinitely, until they click the logout button. Currently I have a session variable loggedin
that checks whether the user is logged in, but session variables expire after some time. How do I keep the user logged in indefinitely?
CodePudding user response:
You can use a cookie for this. When the user logging in for the first time, set a cookie for maybe 10 years. Then, when the user navigates the pages, update/reset the cookie to a further 10 years.
setcookie(
"CookieName",
"CookieValue",
time() (10 * 365 * 24 * 60 * 60)
);
And when the user comes back to the site after some time, check if the cookie is available. If available, you can log them in automatically, if not, redirect to the login page.
if(isset($_COOKIE['CookieName'])){
$cookie = $_COOKIE['CookieName'];
}
else{
// No Cookie found - Redirect to Login
}
You can use two cookies for user name and password(encrypted).