PHP密码表单

时间:2016-04-05 20:41:48

标签: php forms

当我运行这个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>

2 个答案:

答案 0 :(得分:0)

您使用初始值“0”声明了您的$ errors变量。这不是必要的,而是导致问题的原因。

答案 1 :(得分:0)

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