I'm creating a secure login system in PHP.
On each page of the admin area, I want to be sure that the user is correctly logged in, that his account is still enabled, that the user is connected once with his login details (when user loggs in for the first time, the active_user status is set to 1).
function isCookieValid() {
global $pdo;
$isValid = false;
if(isset($_COOKIE["rememberUser"])) {
$decryptCookie = base64_decode($_COOKIE["rememberUser"]);
$user_id = explode("mMUa26yB943jRaJl755OM18jgR", $decryptCookie);
$user_id = $user_id[1];
$sqlQuery = "SELECT * FROM users WHERE id_user = :id";
$stmt = $pdo->prepare($sqlQuery);
$stmt->execute(array(":id" => $user_id));
if ($row = $stmt->fetch()) {
if($row["enabled_user"] === 1 && $row["active_user"] === 0) {
unset($row["password"], $row["salt"]);
$_SESSION["current_user"] = $row;
$isValid = true;
} else {
logout();
}
} else {
logout();
}
}
return $isValid;
}
I ask the database on every page, for every user, to check if the cookie that is stored in the computer (I automatically activate the cookie, because for my application needs to keep all users logged in for a long while) corresponds to an ID user on the database. If so, I create a variable that stores user details. Then for protecting my pages, I use the function isLoggedin() which is:
function isLoggedin() {
if(isCookieValid()){
if(isset($_SESSION["current_user"])) {
return true;
}
}
return false;
}
Is that system not too heavy ? Because I check the database on every page, for every user. Thanks in advance