我的PHP代码中的if条件有问题

时间:2014-08-18 20:56:43

标签: php mysql sql database pdo

我想要做的是当用户点击登录按钮并且输入为空时它将回显please enter email and password,如果用户输入错误的电子邮件和密码,它将回显incorrect email and password

我的问题是,当我点击登录按钮并且输入为空时,它会回显please enter email and passwordincorrect 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 个答案:

答案 0 :(得分:2)

将您的第二个if更改为elseif

答案 1 :(得分:0)

基本上这个:

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