我创建了链接到数据库的登录页面,我想根据用户角色将用户重定向到不同的主页,如果 1 在登录后重定向到 indexorg.php,如果 0 在登录后重定向到 indexpart.php,但它不起作用,所有页面都被重定向到同一页面indexorg.php或indexpart.php(代码中的第一个)
这是我的代码
<?php
// Initialize the session
session_start();
// Check if the user is already logged in, if yes then redirect him to welcome page
if(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 file
require "config.php";
// Define variables and initialize with empty values
$email = $password = "";
$email_err = $password_err = "";
// Processing form data when form is submitted
if($_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"]);
富国沪深