根据用户ID将用户重定向到其他页面

时间:2020-07-27 17:10:03

标签: php mysql

我需要根据用户ID将用户重定向到不同的页面

mysql db用户表如下 “ username = admin”,“ id = 1”和“ username = user”,“ id = 2”

我有两个页面index1.phpindex2.php。我需要在登录管理员打开index1.php和登录用户打开index2.php页面时尝试以下代码,但始终打开一个页面。

<?php
// connect to the database
$db = mysqli_connect('localhost:3308', 'root', '', 'inventory');
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['submit'])) {
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $password = mysqli_real_escape_string($db, $_POST['password']);
    if (empty($username)) {
        array_push($errors, "Username is required");
    }
    if (empty($password)) {
        array_push($errors, "Password is required");
    }
    if (count($errors) == 0) {
        $password = md5($password);
        if (md5($_POST['password']) !== $password) {
            echo "Password is invalid";
        }
        $query = "SELECT * FROM register WHERE username='$username' AND password_1 ='$password'";
        $sql = "SELECT first_name,last_name FROM register WHERE username='$username' AND password_1 ='$password'";
        $result = mysqli_query($db, $sql);
        $row = mysqli_fetch_assoc($result);
        $results = mysqli_query($db, $query);
        $res = mysqli_num_rows($results);
        if ($res) {
            $_SESSION['username'] = $username;
            $_SESSION['first_name'] = $row["first_name"];
            $_SESSION['last_name'] = $row["last_name"];
            header('location: index.php');
        } else {
            array_push($errors, "Wrong username/password combination");
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

if($row["id"]==1)
{
  header('location: index1.php');
}
elseif($row["id"]==2)
{
  header('location: index2.php');
}
else
{
  header('location: index.php');
}