登录到仅管理员页面会将我重定向到登录页面,我在做什么错?

时间:2019-07-09 16:02:42

标签: php authentication admin

我正在尝试为我的网站开发一个博客平台,我有两个用户。我为普通用户设置了一个注释区域,然后管理员可以访问仪表板,他们可以在其中编辑和创建任何内容。但是,只有管理员有权访问该页面。不过,每次我以管理员身份登录时,它都会将我重定向回登录页面,就像我尚未登录一样。

我尝试了来自各个网站的无数其他建议,并通读了所有与我需要的问题有关的问题。谁能将我引导到可以找到有关如何设置此系统的可靠信息的位置,以便我可以登录而无需再次重定向到登录屏幕?

这是我只希望管理员进入的页面之一。

<?php  include('../config.php'); ?>
    <?php include(ROOT_PATH . '/admin/includes/admin_functions.php'); ?>
    <?php include(ROOT_PATH . '/admin/includes/head_section.php'); ?>

    <title>Admin | Dashboard</title>
    <link href="includes/admin_styling.css" rel="stylesheet" type="text/css">
    </head>
<body>
    <div class="header">
        <div class="logo">
            <a href="<?php echo BASE_URL .'admin/dashboard.php' ?>">
                <h1>Administrative Dashboard</h1>
            </a>
        </div>
        <?php if (isset($_SESSION['user'])): ?>
            <div class="user-info">
                <span><?php echo $_SESSION['user']['username'] ?></span> &nbsp; &nbsp; 
                <a href="<?php echo BASE_URL . '/logout.php'; ?>" class="logout-btn">logout</a>
            </div>
        <?php endif ?>
    </div>
    <div class="container dashboard">
        <div class="transbox">
        <h1>Welcome</h1>
        </div>
        <div class="transbox">
        <div class="stats">
            <a href="users.php" class="first">
                <br>
                <span>Registered Users</span>
            </a>
            <a href="posts.php">
            <br>
                <span>Blog Posts</span>
            </a>
            <a>
                <br> <!--WILL ADD IN SOON-->
                <span>Published Comments</span>
            </a>
        </div>
        </div>
        <br><br><br>
        <div class="buttons">
            <a href="users.php">Add Users</a>
            <a href="posts.php">Add Posts</a>
        </div>
    </div>
</body>
</html>

这是registration_login页面:

<?php
    // variable declaration
    $username = "";
    $email    = "";
    $errors = array(); 

    // REGISTER USER
    if (isset($_POST['reg_user'])) {
        // receive all input values from the form
        $username = esc($_POST['username']);
        $email = esc($_POST['email']);
        $password_1 = esc($_POST['password_1']);
        $password_2 = esc($_POST['password_2']);

        // form validation: ensure that the form is correctly filled
        if (empty($username)) {  array_push($errors, "Uhmm...We gonna need your username"); }
        if (empty($email)) { array_push($errors, "Oops.. Email is missing"); }
        if (empty($password_1)) { array_push($errors, "uh-oh you forgot the password"); }
        if ($password_1 != $password_2) { array_push($errors, "The two passwords do not match");}

        // Ensure that no user is registered twice. 
        // the email and usernames should be unique
        $user_check_query = "SELECT * FROM users WHERE username='$username' 
                                OR email='$email' LIMIT 1";

        $result = mysqli_query($conn, $user_check_query);
        $user = mysqli_fetch_assoc($result);

        if ($user) { // if user exists
            if ($user['username'] === $username) {
              array_push($errors, "Username already exists");
            }
            if ($user['email'] === $email) {
              array_push($errors, "Email already exists");
            }
        }
        // register user if there are no errors in the form
        if (count($errors) == 0) {
            $password = md5($password_1);//encrypt the password before saving in the database
            $query = "INSERT INTO users (username, email, password, created_at, updated_at) 
                      VALUES('$username', '$email', '$password', now(), now())";
            mysqli_query($conn, $query);

            // get id of created user
            $reg_user_id = mysqli_insert_id($conn); 

            // put logged in user into session array
            $_SESSION['user'] = getUserById($reg_user_id);

            // if user is admin, redirect to admin area
            if ( in_array($_SESSION['user']['role'], ["Admin", "Author"])) {
                $_SESSION['message'] = "You are now logged in";
                // redirect to admin area
                header('location: ' . BASE_URL . 'admin/dashboard.php');
                exit(0);
            } else {
                $_SESSION['message'] = "You are now logged in";
                // redirect to public area
                header('location: index.php');              
                exit(0);
            }
        }
    }

    // LOG USER IN
    if (isset($_POST['login_btn'])) {
        $username = esc($_POST['username']);
        $password = esc($_POST['password']);

        if (empty($username)) { array_push($errors, "Username required"); }
        if (empty($password)) { array_push($errors, "Password required"); }
        if (empty($errors)) {
            $password = md5($password); // encrypt password
            $sql = "SELECT * FROM users WHERE username='$username' and password='$password' LIMIT 1";

            $result = mysqli_query($conn, $sql);
            if (mysqli_num_rows($result) > 0) {
                // get id of created user
                $reg_user_id = mysqli_fetch_assoc($result)['id']; 

                // put logged in user into session array
                $_SESSION['user'] = getUserById($reg_user_id); 

                // if user is admin, redirect to admin area
                if ( in_array($_SESSION['user']['role'], ["Admin", "Author"])) {
                    $_SESSION['message'] = "You are now logged in";
                    // redirect to admin area
                    header('location: ' . BASE_URL . '/admin/dashboard.php');
                    exit(0);
                } else {
                    $_SESSION['message'] = "You are now logged in";
                    // redirect to public area
                    header('location: index.php');              
                    exit(0);
                }
            } else {
                array_push($errors, 'Wrong credentials');
            }
        }
    }
    // escape value from form
    function esc(String $value)
    {   
        // bring the global db connect object into function
        global $conn;

        $val = trim($value); // remove empty space sorrounding string
        $val = mysqli_real_escape_string($conn, $value);

        return $val;
    }
    // Get user info from user id
    function getUserById($id)
    {
        global $conn;
        $sql = "SELECT * FROM users WHERE id=$id LIMIT 1";

        $result = mysqli_query($conn, $sql);
        $user = mysqli_fetch_assoc($result);

        // returns user in an array format: 
        // ['id'=>1 'username' => 'Awa', 'email'=>'a@a.com', 'password'=> 'mypass']
        return $user; 
    }

?>

那么我如何以及在哪里放置这段代码?我对会议有一定的了解,但是我没有任何尝试。尝试以管理员身份登录时,输入的所有内容都会将我重定向回登录页面。

0 个答案:

没有答案