猿问

使用 PHP 登录/注册的权限

我是 PHP 的新手。我只想问如何在登录时将“管理员”和“用户”帐户重定向到特定页面。

  • 当我以管理员身份登录时,它将被定向到一个页面,如“superuser.php”,我可以在该页面的 GUI 中为“用户”创建一个帐户。

  • 当创建的“用户”帐户登录后,它将被定向到具有不同权限的页面,如“user.php”。

“用户”注册页面与“管理员”不同,因为它将位于管理员的主页。那就是我现在被困的地方

到目前为止,我完成了“管理”部分。

我的PHP代码:

<?php 

    session_start();


    // variable declaration

    $username = "";

    $errors = array(); 

    $_SESSION['success'] = "";


    // connect to database

    $db = mysqli_connect('localhost','root','','registration');

    if (mysqli_connect_errno($db)) {

        echo "Failed to connect to MySQL:" . mysqli_connect_error();

    }


    // REGISTER USER

    if (isset($_POST['reg_user'])) {

        // receive all input values from the form

        $username = mysqli_real_escape_string($db, $_POST['username']);

        $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);

        $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);


        // form validation: ensure that the form is correctly filled

        if (empty($username)) { array_push($errors, "Username is required"); }

        if (empty($password_1)) { array_push($errors, "Password is required"); }


        if ($password_1 != $password_2) {

            array_push($errors, "The two passwords do not match");

        }


        // 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, password) 

                      VALUES('$username', '$password')";

            mysqli_query($db, $query);


            $_SESSION['username'] = $username;

            $_SESSION['success'] = "You are now logged in";

            header('location: login.php');

        }


    }



慕工程0101907
浏览 170回答 2
2回答

烙印99

您需要保存用户权限类型,user_type例如 user、admin、super_user 等CREATE TABLE `users` (`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,`username` varchar(100) NOT NULL,`password` varchar(100) NOT NULL,`user_type` varchar(20) NOT NULL,index(user_type)) ENGINE=InnoDB DEFAULT CHARSET=latin1;当他们登录时,您检查他们user_type并将其存储在会话中 $_SESSION['user_type'] = *their_user_type_from_the_db*然后将他们重定向到正确的页面$_SESSION['user_type'] = *their_user_type_from_the_db*if($_SESSION['user_type']=='user'){&nbsp; &nbsp; header('location: user.php');}elseif($_SESSION['user_type']=='super_user'){&nbsp; &nbsp; header('location: superuser.php');}else{&nbsp; &nbsp; header('location: home.php');}

呼啦一阵风

这可以在数据库上轻松处理,并允许您控制用户类型,而无需使用检查常规登录会话之外的会话的特殊代码。如果您有一个用户表,请在表中放置一列用于type. 在表中设置一个与他们的用户类型相对应的值。对于普通用户,将该值设置为 1,对于“超级用户”或“管理员”,将其设置为 2。当他们登录时查询数据库并检查他们的用户类型,以及是否将其设置为普通用户的值,只允许他们访问常规用户数据,如果设置为管理员,则允许他们访问管理部分。在您获得用户登录的结果后,将执行以下查询:$sql = "SELECT usertype FROM users WHERE username='$username'";在将 $row 信息收集到数组中后,检查$row['usertype'].if($row['userType'] === 2){&nbsp; &nbsp; //Admin user permissions&nbsp; &nbsp; //Set an admin session here&nbsp; &nbsp; header('Location: admin.php');&nbsp; &nbsp; exit();}else{&nbsp; &nbsp; //Regular user permissions&nbsp; &nbsp; //Set an reg user session here&nbsp; &nbsp; header('Location: loginHome.php');&nbsp; &nbsp; exit();}
随时随地看视频慕课网APP
我要回答