登录系统错误 - PDO

时间:2013-08-23 23:42:39

标签: php html5 login pdo

我的登录系统使用PDO时出现问题。当您使用正确的密码登录时,即使表单链接到另一个页面,它也只会刷新页面。当你登录时,你将被重定向到" home.php"不是" index.php"

<?php
include('./includes/connect.php');

$submit = $_POST['submit'];
$email = sanitize($_POST['email']);
$password = nCrypt(sanitize($_POST['password']));

if(isset($submit)) {
    if(isset($email) && isset($password)) {
        $query = $pdo->prepare("SELECT password FROM users WHERE email = ?");
        $query->bindValue(1, $email);
        $query->execute();

        $r = $query->fetch();
        $pass2 = $r['password'];
        $first = $r['first'];
        $last = $r['lastname'];
        $username = $r['username'];

        if(strcasecmp($password, $pass2) == 0) {
            $_SESSION['user'] = $r['user'];
            $_SESSION['first'] = $r['first'];
            $_SESSION['last'] = $r['last'];
            $_SESSION['id'] = $r['id'];
            header('Location: home.php');
        } else {
            header('Location: index.php?e=incorrect');
        }
    } else {
        header('Location: index.php?e=empty');
    }
}
?>

基本上,当您输入正确的密码时,没有任何反应。我的表格是:

<!-- index.php -->
<form action="login.php" method="POST">
    <legend>Email Address</legend>
    <input type="email" name="email" placeholder="Email Address" required><br>
    <legend>Password</legend>
    <input type="password" name="password" placeholder="Password" required><br>
    <input class="btn btn-info" type="submit" name="submit" value="Login">
</form>

正如您所看到的,它链接到另一个页面,但是当您输入正确的密码时,它只会刷新表单所在的页面,当它想要将您重定向到&#34; home时。 PHP&#34;

1 个答案:

答案 0 :(得分:1)

如果您要选择此声明

SELECT password FROM users WHERE email = ?

那你要去哪里

$first = $r['first'];
$last = $r['lastname'];
$username = $r['username'];

这?

做一个

SELECT * FROM users WHERE email = ?

OR

SELECT password, first,last,username FROM users WHERE email = ?

另外,为什么复杂的生活会很简单,如果要测试正确的密码:

if($password == $pass2) {

同样值得检查的是,数据库上的密码实际上是以加密用户输入的方式加密的。