如何根据用户的状态和角色登录用户

我正在创建一个登录页面,用户和管理员将在其中登录用户将具有角色 = 用户和状态 = 待定,直到管理员将其激活。我有不同的文件要显示给用户和管理员,在用户中,有 2 个文件。1 个用于活动用户,另一个用于待定用户。


我创建了 if 语句并尝试了 switch 语句。但我在 XAMPP 上遇到错误“解析错误:语法错误,C:\xampp\htdocs\MakerLab\server.php 中第 109 行的文件意外结束”


这是我的 server.php


...


<?php 

    session_start();


    // variable declaration

    $email = "";

    $status = "";


    $errors = array(); 

    $_SESSION['success'] = "";


    // connect to database

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


    // REGISTER USER

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

        // receive all input values from the form

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

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

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

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

        $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($email)) { array_push($errors, "Lewis Email 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");

        //}


    $user_check_query = "SELECT * FROM users WHERE lewisID='$lewisID' OR email='$email' LIMIT 1";

    $result = mysqli_query($db, $user_check_query);

    $user = mysqli_fetch_assoc($result);


    if ($user) { // if user exists

    if ($user['lewisID'] === $lewisID) {

    array_push($errors, "lewisID already exists");

    }


    if ($user['email'] === $email) {

    array_push($errors, "lewisID 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 (lewisID,


holdtom
浏览 147回答 1
1回答

慕妹3242003

您在文件末尾缺少 2 个括号(在 ?> 标记之前)下次您可以使用像 PHPStorm 这样的 IDE 来帮助缩进和格式化。<?php// variable declaration$email = "";$status = "";$errors = array();$_SESSION['success'] = "";// connect to database$db = mysqli_connect('localhost', 'root', '', 'makerlab');// REGISTER USERif (isset($_POST['reg_user'])) {&nbsp; &nbsp; // receive all input values from the form&nbsp; &nbsp; $fname = mysqli_real_escape_string($db, $_POST['fname']);&nbsp; &nbsp; $lname = mysqli_real_escape_string($db, $_POST['lname']);&nbsp; &nbsp; $email = mysqli_real_escape_string($db, $_POST['email']);&nbsp; &nbsp; $lewisID = mysqli_real_escape_string($db, $_POST['lewisID']);&nbsp; &nbsp; $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);&nbsp; &nbsp; $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);&nbsp; &nbsp; // form validation: ensure that the form is correctly filled&nbsp; &nbsp; //if (empty($email)) { array_push($errors, "Lewis Email is required"); }&nbsp; &nbsp; //if (empty($password_1)) { array_push($errors, "Password is required"); }&nbsp; &nbsp; //if ($password_1 != $password_2) {&nbsp; &nbsp; //&nbsp; array_push($errors, "The two passwords do not match");&nbsp; &nbsp; //}&nbsp; &nbsp; $user_check_query = "SELECT * FROM users WHERE lewisID='$lewisID' OR email='$email' LIMIT 1";&nbsp; &nbsp; $result = mysqli_query($db, $user_check_query);&nbsp; &nbsp; $user = mysqli_fetch_assoc($result);&nbsp; &nbsp; if ($user) { // if user exists&nbsp; &nbsp; &nbsp; &nbsp; if ($user['lewisID'] === $lewisID) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($errors, "lewisID already exists");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ($user['email'] === $email) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($errors, "lewisID already exists");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // register user if there are no errors in the form&nbsp; &nbsp; if (count($errors) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; $password = md5($password_1);//encrypt the password before saving in the database&nbsp; &nbsp; &nbsp; &nbsp; $query = "INSERT INTO users (lewisID,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fname,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lname,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VALUES('$lewisID',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '$fname',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '$lname',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '$email',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '$password')";&nbsp; &nbsp; &nbsp; &nbsp; mysqli_query($db, $query);&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['fname'] = $fname;&nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['email'] = $email;&nbsp; &nbsp; &nbsp; &nbsp; header('location: pend.php');&nbsp; &nbsp; }}// ...// LOGIN USERif (isset($_POST['login_user'])) {&nbsp; &nbsp; $email = mysqli_real_escape_string($db, $_POST['email']);&nbsp; &nbsp; $password = mysqli_real_escape_string($db, $_POST['password']);&nbsp; &nbsp; if (empty($email)) {&nbsp; &nbsp; &nbsp; &nbsp; array_push($errors, "Lewis Email is required");&nbsp; &nbsp; }&nbsp; &nbsp; if (empty($password)) {&nbsp; &nbsp; &nbsp; &nbsp; array_push($errors, "Password is required");&nbsp; &nbsp; }&nbsp; &nbsp; if (count($errors) == 0) {&nbsp; &nbsp; &nbsp; &nbsp; $password = md5($password);&nbsp; &nbsp; &nbsp; &nbsp; $query = "SELECT * FROM users WHERE email='$email'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; AND password='$password'";&nbsp; &nbsp; &nbsp; &nbsp; $results = mysqli_query($db, $query);&nbsp; &nbsp; &nbsp; &nbsp; if (mysqli_num_rows($results) == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['email'] = $email;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $row = mysqli_fetch_assoc($results);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $status = $row['status'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $role = $row['role'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($status == "Pending") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header('location: pend.php');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ($status == "Active" || $role == "user") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header('location: AccountMain.php');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if ($status == "Active" || $role == "admin") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header('location: admain.php');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($errors, "Wrong username/password combination");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP