How to keep a user session active even after closing the browser or returning after being idle in PHP for a while?
<p>In my code I can select "Remember me", when selected it saves the value of email and password in a cookie and the cookie lasts for 1 month, now when the user enters the page again, Instead of keeping the session active, it gets the value from the saved cookie and displays it on the login form.
I'm currently using cloudflare and hosting and I've tried setting up these lines of code: </p>
<pre class="brush:php;toolbar:false;">php_value session.cookie_lifetime 2629800;
php_value session.gc_maxlifetime 2629800;</pre>
<p>Both are in php.ini and .htacces, but the result is the same (the session will be closed when the user closes the browser). </p>
<p>This is the loginUser function, it handles the login and creates the cookie when the user checks "Remember Me", what I want is, by checking "Remind Me", even if the user closes the browser or comes back after some time, The session remains active for the inactivity time, but the session duration is 1 month (2629800 seconds). </p>
<pre class="brush:php;toolbar:false;">function loginUser($email, $password, $rememberme)
{
$mysqli = connect();
$email = trim($email);
$password = trim($password);
if ($email == "" || $password == "") {
return 'Both fields are required';
}
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$password = filter_var($password, FILTER_SANITIZE_SPECIAL_CHARS);
$sql = "SELECT * FROM users WHERE email = ? LIMIT 1";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
if ($data == NULL) {
return 'The email does not exist, sign up to enter';
}
if (password_verify($password, $data["password"]) == FALSE) {
return 'The password is incorrect, please check and try again';
} else {
$user_id = $data['user_id'];
$_SESSION['auth_user_id'] = $user_id;
$_SESSION["user"] = $email;
$_SESSION["pass"] = $password;
$_SESSION["username"] = $data['username'];
$_SESSION["verify"] = $data['verify'];
$_SESSION["profile"] = $data['profileImage'];
$_SESSION["id"] = $data['user_id'];
$_SESSION["vip"] = $data['vip'];
setcookie('user_id', $data['user_id'], time() 60 * 60 * 24 * 30, '/');
if ($rememberme) {
$cookie_name = 'Remember_US';
$cookie_value = json_encode(array('lemail' => $email, 'lpassword' => $password));
$cookie_expire = time() (60 * 60 * 24 * 30);
setcookie($cookie_name, encrypt($cookie_value), $cookie_expire, '/', null, true, true);
}
header("location: index.php");
exit();
}
}</pre>
<p>PS: I know this is a duplicate, but none of the questions above solved my problem.</p>
<p>I mention again that I tried setting these lines in htaccess and php.ini with no success: </p>
<pre class="brush:php;toolbar:false;">php_value session.cookie_lifetime 2629800;
php_value session.gc_maxlifetime 2629800;
//In functions.php
$expire = 60*60*24*30; // We choose a one year duration
ini_set('session.gc_maxlifetime', $expire);
session_start();</pre>
<p>It's also worth mentioning that by establishing lines of code when closing and reopening the browser, the session remains active, but only for a maximum of 2 hours. </p>