简体   繁体   English

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

[英]php- redirection works on only one page

so I tried using the code from Andy Holmes from this link How to redirect into different page by user type in php and mysql . 所以我尝试使用来自此链接的Andy Holmes的代码如何通过php和mysql中的用户类型重定向到不同的页面

Server.php 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");
    }
  }

?>

But the problem is it works for only one page and It keeps redirecting to the admin page even if the occupation is not admin. 但是问题是它仅适用于一个页面,即使职业不是管理员,它也会一直重定向到管理页面。

Unless occupation equals student, it redirects to admin and $occupation = $row['$occupation'] . 除非职业等于学生,否则它将重定向到admin和$occupation = $row['$occupation'] However, I don't see you set the value of $row anywhere. 但是,我看不到您在任何地方设置$row的值。 So, the redirect will always be to admin. 因此,重定向将始终是到管理员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM