使用PDO验证密码没有任何回复

时间:2015-05-01 10:54:58

标签: php pdo login password-protection

我正在尝试创建一个登录页面但是当我尝试登录时,我得到一个空白页面,我也没有错误。我想根据登录脚本的结果返回成功或失败消息。我哪里错了?

登录页面:

try {
    $dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);
    } catch   (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
$username   = $_POST['username'];
$password   = $_POST['password'];

 $sql = "SELECT * FROM users WHERE username = : username LIMIT 1";
$query = $dbh->prepare( $sql );
$query->execute( array( ':username'=>$username ) );
$results = $query->fetchAll( PDO::FETCH_ASSOC ); 

foreach( $results as $row ){ 
     if(password_verify($_POST['password'], $password)){
        echo 'verified';
    }
    else
    {
        echo 'not verified';
    }
    }

创建用户的页面:

$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$hash = password_hash($password, PASSWORD_DEFAULT);
//sets type to '1' which is used for a user.
$stmt = $dbh->prepare("insert into users set username=?, email=?, password=?, type= '1'");
$stmt->execute([$username, $email, $hash]);


echo "<p>Thank you for registering!</p>";

1 个答案:

答案 0 :(得分:4)

冒号和“用户名”之间有一个空格

'username = : username'
             ^

不应有任何空格,并以:username

的形式阅读

您正在使用以下几行:

array( ':username'=>$username )