PurpleSmurph
PurpleSmurph

Reputation: 2107

PHP password validation error

I'm creating a login for a website using PHP but when I try to login I always get the same error "You forgot to enter your password." An error I included if the user forgets to enter the password, however I have been entering a password. Any help would be greatly appreciated.

The 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

The HTML form:

     <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>

Thank in advance!

Upvotes: 1

Views: 1190

Answers (4)

Francis Chen
Francis Chen

Reputation: 35

You logic is wrong. You code show that "If you pass1 is EMPTY, compare the pass1 AND pass2" The correct code is

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

Upvotes: 0

UncleKing
UncleKing

Reputation: 743

//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.';
}
....

Upvotes: 0

Michael
Michael

Reputation: 12806

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.';
}

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Your code says to send the error message if the password is NOT empty (else case of empty). Either switch the then and else blocks around, or put a ! before empty.

Upvotes: 2

Related Questions