【问题标题】:php- redirection works on only one pagephp-重定向仅适用于一页
【发布时间】:2018-11-18 07:02:42
【问题描述】:

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

服务器.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");
    }
  }

?>

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

【问题讨论】:

  • 除非职业等于学生,否则它会重定向到管理员和$occupation = $row['$occupation']。但是,我没有看到您在任何地方设置 $row 的值。因此,重定向将始终指向管理员。
  • 非常感谢您的评论。我一直在处理这个问题,你的洞察力有所帮助
  • 我很高兴能够提供帮助。我想我应该将其添加为答案,以便其他查看此问题的人更容易找到它。随意接受它并使用您的任何发现对其进行编辑。
  • 是的,请这样做。我已经合并了,非常感谢您的帮助
  • 并包含另一个代码有助于使其工作

标签: php


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-24
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多