我正在用PHP创建安全的登录系统。在管理区域的每个页面上,我要确保用户已正确登录,他的帐户仍处于启用状态,该用户使用他的登录详细信息进行了一次连接(当用户首次登录时,active_user状态设置为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;
}
我要求每一页上的数据库为每个用户检查存储在计算机中的cookie(我会自动激活该cookie,因为对于我的应用程序,它需要让所有用户长时间登录)对应于数据库上的ID用户。如果是这样,我将创建一个存储用户详细信息的变量。然后,为了保护我的页面,我使用了函数[[isLoggedin(),它是:
function isLoggedin() {
if(isCookieValid()){
if(isset($_SESSION["current_user"])) {
return true;
}
}
return false;
}
那个系统不是太重吗?因为我检查每个用户的每一页上的数据库。在此先感谢
function isLoggedin() {
if(isset($_SESSION["current_user"])) {
return true;
} else {
// here redirect to login page to verify the user id and password..
}
}
<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["current_user"]) || $_SESSION["current_user"] !== true){
header("location: login.php");
exit;
}
?>