Php 登录表单无法使用我的代码

Php 登录表单无法使用我的代码,php,database,Php,Database,数据库正在运行,但当我尝试使用我注册的信息登录时,它只会告诉我信息不正确,就像我告诉它说的那样,但我知道信息是正确的。我没有看到下面的问题,所以任何帮助都是100%感谢。如果你需要更多的代码,我可以发布 if (isset($_POST["user_login"]) && isset($_POST["password_login"])) { $user_login = preg_replace('#[^A-Za-z0-9]#i', ' ', $_POST["user_lo

数据库正在运行,但当我尝试使用我注册的信息登录时,它只会告诉我信息不正确,就像我告诉它说的那样,但我知道信息是正确的。我没有看到下面的问题,所以任何帮助都是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");
?>

Crypt的工作方式与您使用它的方式不同。每次它都会返回不同的散列。您可能希望使用md5函数进行替换,或者查看crypt的使用示例

这是我通常用于crypt Requires PHP>=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;

因为您已经生成了散列,所以应该能够使用验证部分来检查您的登录/密码是否正确。只需使用用户名从数据库中取出散列,并执行$valid=crypt$password,$hash===$hash

回显$userCount时会发生什么情况?此外,您还可以包括连接代码。您可以屏蔽登录信息。为什么要从密码中筛选字符?您的查询很可能返回0条记录。检查用户名和密码组合是否存在。尤其要检查echo$password\u login\u crypt;并确保它与中的相同database@Eli我发布了我的连接代码。我的注册表格将信息输入数据库,所以它可以正常工作,只是登录不起作用。我以前在那里有md5,但我被告知md5很可怕。你知道比md5更好的吗?我在注册表单代码中使用了crypt,可以吗?我的意思是,它可以工作,所以只是想知道。你是对的,crypt会比md5更安全。我用一个例子更新了我的答案。