为什么这段代码会重定向到a.p p,而不管我输入的密码和用户名


Why does this piece of code redirect to a.php irrespective of the password and username i input?

提交表单的代码:

</form>
<script>
var user = prompt('Enter username');
var pass = prompt('Enter password');
document.getElementById('user').value = user;
document.getElementById('pass').value = pass;
document.getElementById('form').submit();
</script>
验证用户名和密码的代码:
   <?php
if($_POST['user'])
{
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    if($user = "root")
    {
    if($pass = "pass")
    {
        header('Location: a.php');
    }
    else
    {
        echo'<b>Access Denied</b>'; 
    }
    }
else
    {
        echo'<b>Access Denied</b>';
    }
}   
?>

即使我在提示框中键入的用户名和密码分别不是'root'和'pass', valid.php页面也会预测到a.p p,我不知道为什么。

if($user = "root") 
应该

if($user == "root")

等等

= assignment
== / === comparison 

你需要纠正这些条件。

了解更多比较运算符

使用==比较而不是=赋值…所有人都会犯的正常错误!!

可以使用单个if else语句

if($user == "root"  && $pass == "pass" )
        {
            header('Location: a.php');
        }
    else
        {
            echo'<b>Access Denied</b>';
        }