登录表单不适用于我的代码

时间:2014-03-28 21:56:01

标签: php database

数据库正在运行,但当我尝试使用我注册的信息登录时,只是告诉我信息不正确,因为我告诉它说但是我知道信息是正确的。我没有看到下面的问题,所以任何帮助都是100%赞赏。如果您需要查看更多可以发布的代码。

if (isset($_POST["user_login"]) && isset($_POST["password_login"])) {
    $user_login = preg_replace('#[^A-Za-z0-9]#i', ' ', $_POST["user_login"]); // filter everything but numbers and letters
    $password_login = preg_replace('#[^A-Za-z0-9]#i', ' ', $_POST["password_login"]); // filter everything but numbers and letters
    $password_login_crypt = crypt($password_login);
    $sql = mysql_query("SELECT id FROM users WHERE username='$user_login' AND password='$password_login_crypt' LIMIT 1"); // query
       //Check for their existence
       $userCount = mysql_num_rows($sql); //Count the number of rows returned
       if ($userCount == 1) {
           while($row = mysql_fetch_array($sql)){
                    $id = $row["id"];
       }
           $_SESSION["user_login"] = $user_login;
           header("location: home.php");
           exit();
           } else {
           echo 'That information is incorrect, try again';
           exit();
       }
}

?>
                <div style="width: 800px; margin: 0px auto 0px auto;">
                <table>
                        <tr>
                            <td width="60%" valign="top">
                                  <h2>Already a member? Sign in below!</h2>
                                  <form action="index.php" method="POST">
                                         <input type="text" name="user_login" size="25" placeholder="Username" /><br /><br />
                                         <input type="text" name="password_login" size="25" placeholder="Password" /><br /><br />
                                         <input type="submit" name="login" size="25" value="Login!" />
                                  </form>

我的连接代码。标记-----我的个人信息确实在哪里。

<?php
mysql_connect("-----","-----", "-----") or die("Couldn't connect to SQL server");
mysql_select_db("-----") or die("Couldn't select DB");
?>

1 个答案:

答案 0 :(得分:1)

Crypt无法按照您使用它的方式运行。它每次都会返回一个不同的哈希值。您可能希望使用md5函数进行替换,或者查看PHP crypt documentation以获取crypt的使用示例。

这是我通常用于crypt的代码(需要PHP&gt; = 5.3.0):

//Generate
$password = 'password';
$salt = bin2hex(openssl_random_pseudo_bytes(22));
$hash = crypt($password, '$2y$10$' . $salt);

//Validate
$valid = crypt($password, $hash) === $hash;

由于您已经生成了哈希,因此您应该能够使用validate部分来检查您的登录名/密码是否正确。只需使用用户名从数据库中获取哈希值并执行$valid = crypt($password, $hash) === $hash;