登录表单不适用于我的代码 [英] Login form is not working with my code

查看:49
本文介绍了登录表单不适用于我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The database is working, but when I try to login with the info I signed up with it just tells me information is incorrect as I told it to say that but I know info is right. I do not see a problem below so any help is 100% appreciated. If you need to see more code I can post.

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>

我的连接代码。标记为-----我的个人信息的真实位置。

My connection code. Marked ----- where my personal information really is.

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


推荐答案

土窖无法按您使用的方式工作。每次都会返回不同的哈希值。您可能需要使用md5函数进行直接替换,或查看 PHP加密

Crypt does not work the way you are using it. It will return a different hash each time. You may want to use the md5 function for a drop-in replacement, or look at the PHP crypt documentation for usage examples of crypt.

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

This is the code I usually use for 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;

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

Since you already generated the hash, you should be able to use the validate part to check that your login/password is correct. Just use the username to get the hash out of the database and do $valid = crypt($password, $hash) === $hash;

这篇关于登录表单不适用于我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆