1

I want to do is when a user click the login button and the inputs are empty its will echo please enter email and password and if the user put email and password wrong it will echo incorrect email and password .

My problem is when i click the login button and the inputs are empty it echo both please enter email and password and incorrect email and password.

I want to echo please enter email and password if both email and password are empty only and if one of email or password is wrong it should echo incorrect email and password.

<?php

require 'encryption.php';
include_once('database.php');

$db = new Connection();
$db = $db->dbConnect();
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$email = $_POST['email'];
$pass = $_POST['password'];

$hash_cost_log2 = 8;
$hash_portable = FALSE;
$hasher = new PasswordHash($hash_cost_log2, $hash_portable);
$hash = '*';

$login = $db->prepare("SELECT user_password FROM tbl_user WHERE user_email= :email");
$login->bindParam(':email', $email, PDO::PARAM_STR);
$login->execute();
$hash = $login->fetchColumn();

    if(empty($email) && empty($pass)){
      echo "Please enter Email and Password";
    }
    if($hasher->CheckPassword($pass, $hash))
    {
        $st = $db->prepare("SELECT * from tbl_user WHERE user_email=? AND user_password=? AND user_active='1'");
        $st->bindParam(1, $email, PDO::PARAM_STR);
        $st->bindParam(2, $hash, PDO::PARAM_STR);
        $st->execute();

        if($st->rowCount() === 1){
            echo "1";
            exit;
        }
    }else{
        echo "Incorrect Email or Password";  
    }  
?>
2
  • 2
    if(empty($email) || empty($pass)) if one or the other is empty, it will trigger the error. Commented Aug 18, 2014 at 20:58
  • Either that, or check it on the client side with JS before passing it along to the PHP. Commented Aug 18, 2014 at 20:59

2 Answers 2

2

Change your second if to elseif.

Sign up to request clarification or add additional context in comments.

Comments

0

Basically this:

if(empty($email) && empty($pass)){
    echo "Please enter Email and Password";
} else {
   if ($hasher->CheckPassword($pass, $hash))
         echo "works"
   } else {
         echo "doesn't work"
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.