为什么该语句在if语句之前运行?

时间:2019-07-29 01:26:04

标签: php mysqli

这是我的代码:

if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['username']) && isset($_POST['pass']) && isset($_POST['repeat-pass'])){

  $fullname = mysqli_real_escape_string($con, $_POST['name']);
  $email = mysqli_real_escape_string($con, $_POST['email']);
    $username = mysqli_real_escape_string($con, $_POST['username']);
  $password_post = $_POST['pass'];
  $confirmpassword_post = $_POST['repeat-pass'];
    $password = mysqli_real_escape_string($con, hash('sha3-512' , "$password_post"));
    $confirmpassword = mysqli_real_escape_string($con, hash('sha3-512' , "$confirmpassword_post"));

    if($password != $confirmpassword){
        header("Location: index.php?failed=2");
    }

    $result = mysqli_query($con, "SELECT * FROM `users` WHERE `username` = '$username'") or die(mysqli_error($con));
    if(mysqli_num_rows($result) > 0){
    header("Location: index.php?failed=1");
    }

    $result = mysqli_query($con, "SELECT * FROM `users` WHERE `email` = '$email'") or die(mysqli_error($con));
    if(mysqli_num_rows($result) > 0){
         header("Location: index.php?failed=3");
    }

    $ip = mysqli_real_escape_string($con, $_SERVER['REMOTE_ADDR']);
    $date = date('Y-m-d');

   mysqli_query($con, "INSERT INTO `users` (`full_name`, `email`, `username`, `password`, `date`, `ip`) VALUES ('$fullname', '$email', '$username', '$password', '$date', '$ip')") or die(mysqli_error($con));
    header("Location: index.php?success=1"); 
}

我的问题是,当两个密码不相等时,fi语句应将用户发送到index.php?failed = 2。 它不起作用,它将首先运行mysqli_query(Insert)并将用户重定向到index.php?success = 1。 但是,为什么如果if陈述在另一个陈述之前,为什么它在成功重定向之后运行呢?

1 个答案:

答案 0 :(得分:2)

仅发送位置标头将不起作用,因为PHP将继续执行脚本。

您需要在每个die();调用之后添加一个header,以确保PHP停止评估其余代码。