PHP密码表单


PHP password form

当我运行这个php脚本时,它在底部回显一个错误"0"。当我输入正确的名称和密码时,它会让我进入正确的页面。同样,当我输入错误的用户和密码时,会显示正确的消息。为什么有首字母"0"?

<!DOCTYPE html>
<html lang = "en">
    <head>
        <meta charset ="utf-8">
            <title>Login page</title>
    </head>
    <body>
    <h1>Log in to our Website here:</h1>
    <?php 
    $self = htmlentities($_SERVER['PHP_SELF']);
    $creds = array(
                array('username' => 'Bob',
                        'password' => 'bill'),
                array('username' => 'mary',
                        'password' => 'jane')
                );
    $match = false;
    $errors = $username = $password = 0;
    if (isset($_POST['username']) && isset($_POST['password'])) {
        $details_entered = true;
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);
            for ($i=0; $i<count($creds); $i++) {
                if(strcmp($username, $creds[$i]['username']) ==0) {
                    if(strcmp($password, $creds[$i]['password']) ==0) {
                session_start();
                $_SESSION['authenticated'] = true;
                $_SESSION['username'] = $creds[$i]['username'];
                $match = true; 
            } 
        } else {
            $errors = "***Either your username or password are not correct***";
        }
    }
}
if($match) {
    header ('location: login.php');
}
?>
        <form id = "login" action ="<?php echo $self; ?>" method ="post">
        <fieldset>
        <p>
        <label for ="username">*UserName:</label>
        <input type ="text" name ="username" id ="username" maxlength ="50" placeholder ="Username"/>
        </p>
        <p>
        <label for = "password">*Password:</label>
        <input type ="password" name ="password" id="password" maxlength ="50" placeholder ="Password"/>
        </p><span><?php echo $errors; ?></span>
        <p>
        <input type ="submit" name ="Login" value ="Login"/>
        </p>
        <p>
        <a href = "register.html">Or register with us here:</a>
        </p>
        </fieldset>
        </form>
    </body>
</html>

尝试将$errors = $username = $password = 0;更改为$errors = $username = $password = "";这应该可以解决问题。

您用初始值"0"声明了$errors变量。这不是必要的,这就是问题的原因。