我創建了鏈接到數據庫的登錄頁面,我想根據用戶角色將用戶重定向到不同的主頁,如果 1 在登錄后重定向到 indexorg.php,如果 0 在登錄后重定向到 indexpart.php,但它不起作用,所有頁面都被重定向到同一頁面indexorg.php或indexpart.php(代碼中的第一個)這是我的代碼<?php// Initialize the sessionsession_start(); // Check if the user is already logged in, if yes then redirect him to welcome pageif(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){ if ($_SESSION["user_role"] = "1") { $redirect = 'indexorg.php'; } else if ($_SESSION["user_role"] == "0") { $redirect = 'indexpart.php'; } header('Location: ' . $redirect); //header("location: index.php"); exit;} // Include config filerequire "config.php"; // Define variables and initialize with empty values$email = $password = "";$email_err = $password_err = ""; // Processing form data when form is submittedif($_SERVER["REQUEST_METHOD"] == "POST"){ // Check if email is empty if(empty(trim($_POST["email"]))){ $email_err = "Please enter email."; } else{ $email = trim($_POST["email"]); } // Check if pass is empty if(empty(trim($_POST["password"]))){ $password_err = "Please enter your password."; } else{ $password = trim($_POST["password"]); } // Validate credentials if(empty($email_err) && empty($password_err)){ // Prepare a select statement $sql = "SELECT user_role, user_id, email, password FROM users WHERE email = :email"; if($stmt = $pdo->prepare($sql)){ // Bind variables to the prepared statement as parameters $stmt->bindParam(":email", $param_email, PDO::PARAM_STR); // Set parameters $param_email = trim($_POST["email"]);
1 回答

富國滬深
TA貢獻1790條經驗 獲得超9個贊
在這一行
if ($_SESSION["user_role"] = "1")
您實際上將“1”分配給 $_SESSION["user_role"] 這始終是正確的。
為了進行比較,您應該使用==
或,!=
就像您在第二次比較中使用的那樣。
- 1 回答
- 0 關注
- 96 瀏覽
添加回答
舉報
0/150
提交
取消