亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

具有不同重定向的php登錄頁面

具有不同重定向的php登錄頁面

PHP
互換的青春 2023-10-01 10:08:17
我創建了鏈接到數據庫的登錄頁面,我想根據用戶角色將用戶重定向到不同的主頁,如果 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"] 這始終是正確的。

為了進行比較,您應該使用==或,!=就像您在第二次比較中使用的那樣。


查看完整回答
反對 回復 2023-10-01
  • 1 回答
  • 0 關注
  • 96 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號