php-重定向仅在一页上有效

时间:2018-11-18 07:02:42

标签: php

因此,我尝试通过此链接How to redirect into different page by user type in php and mysql使用来自Andy Holmes的代码。

Server.php

    <?php
session_start();

// initializing variables
$username = "";
$email    = "";
$errors = array(); 

// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration');

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

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($email)) { array_push($errors, "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");
  }
  if (empty($occupation)) { array_push($errors, "Occupation is required"); }
  if (empty($grdlvl)) { array_push($errors, "Grade level Applied is required"); }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $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");
    }
  }

  // Finally, 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, occupation, grdlvl) 
              VALUES('$username', '$email', '$password', '$occupation', '$grdlvl')";
    mysqli_query($db, $query);
    $_SESSION['username'] = $username;
    $_SESSION['success'] = "You are now registered";
    header('location: login.php');
  }
}

// ... 

// ... 

// LOGIN USER
if (isset($_POST['login_user'])) {
  $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);
    $query = "SELECT * FROM users WHERE username='$username'AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in";
}
$occupation = $row['$occupation'];

if($occupation == "student"){ //check usertype

    header("Location:/site2/student.php"); //if normal user redirect to app.php

    }else{

    header("Location:/site2/admin.php"); //if admin user redirect to admin.php
     }
}

    else {
      array_push($errors, "Wrong username/password combination");
    }
  }

?>

但是问题是它仅适用于一页,即使该职业不是管理员,它也会一直重定向到管理页面。

1 个答案:

答案 0 :(得分:0)

除非职业等于学生,否则它将重定向到admin和$occupation = $row['$occupation']。但是,我看不到您在任何地方设置$row的值。因此,重定向将始终是到管理员。

相关问题