PHP密码验证错误

时间:2012-03-25 13:25:35

标签: php

我正在使用PHP创建网站登录但是当我尝试登录时,我总是会收到相同的错误“您忘了输入密码。”如果用户忘记输入密码,我包含的错误,但是我一直在输入密码。任何帮助将不胜感激。

PHP:

//check for a password and a match against the confirmed password:
if (empty($_POST['pass1']))
{
    if($_POST['pass1'] != $_POST['pass2'])
    {
        $errors[] = 'Your passwords did not match.';
    }
    else
    {
        $p = trim($_POST['pass1']);
    }
}
else
{
    $errors[] = 'Your forgot to enter your password.';
}

if (empty($errors)) //if everything is okay
{
//register the user in the database
require_once
('../mysqli_connect.php'); //connect to the DB

HTML表单:

     <p>Password: <input type="password" name="pass1" size="15" maxlength="20"/>*</p>

     <p>Confirm Password: <input type="password" name="pass2" size="15" maxlength="20"/>*</p>

提前感谢!

4 个答案:

答案 0 :(得分:2)

如果密码不为空(else为空,则代码表示发送错误消息)。可以切换thenelse块,也可以在!之前加empty

答案 1 :(得分:0)

if (!empty($_POST['pass1']))
{
    if($_POST['pass1'] != $_POST['pass2'])
    {
        $errors[] = 'Your passwords did not match.';
    }
    else
    {
        $p = trim($_POST['pass1']);
    }
}
else
{
    $errors[] = 'Your forgot to enter your password.';
}

答案 2 :(得分:0)

//check for a password and a match against the confirmed password:
if (!empty($_POST['pass1']))
{
    if($_POST['pass1'] != $_POST['pass2'])
    {
        $errors[] = 'Your passwords did not match.';
    }
    else
    {
        $p = trim($_POST['pass1']);
    }
}
else 
{
    $errors[] = 'Your forgot to enter your password.';
}
....

答案 3 :(得分:0)

你的逻辑错了。 您的代码显示“如果您传递1 EMPTY ,请比较pass1pass2” 正确的代码是

if (!empty($_POST['pass1']))
{....}
else{....}