0

我是 mysql 和 php 的新手。

一直致力于为用户创建一个带有表的数据库。

我已经成功地将用户添加到数据库,他们的密码使用 md5(是的,我知道它不安全),它不会在线启动。

我的问题是,如何根据用户正确的用户名和密码登录用户。

这是我的代码

查询运行后我的逻辑是正确的,它将返回真或假。

如果为真,则显示登录成功,否则显示登录失败。

但是,即使我输入了正确的用户名和密码,我仍然会收到不成功的登录消息

我检查了mysql数据库,并且uesrname在那里正确

想法?

if(!empty($_POST['userLog']) && !empty($_POST['passLog']))
{
    //set the username and password variables from the form
    $username = $_POST['userLog'];
    $password = $_POST['passLog'];

    //create sql string to retrieve the string from the database table "users"
    $sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = md5('$password')";
    $result = mysql_query($sql);
        if ($result == true) {
            $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>";
        } else {
            $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
        }
        print($return);
}
4

4 回答 4

1

我不完全确定你的 SQL 会运行,但只是为了安全起见。

改变它,以便

$password_hash = md5($password);

$sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = '$password_hash'";

对于你原来的问题

if(mysql_num_rows($result) == 1) { //If the SQL returns one row, that means that a user was found with `userName = $username` and `password = md5($password)`
    // Login
} else {
    // Authentication Failed
}

此外,考虑使用 MySQLi 而不是 MySQL,因为它已被贬值。

于 2013-03-02T21:41:59.720 回答
0

首先,保护您的代码免受SQL 注入

然后,确保数据库中的密码确实使用 md5() 函数进行了哈希处理。确保您的表单使用 POST 方法将数据传递给脚本。

试试下面的代码:

if(!empty($_POST['userLog']) && !empty($_POST['passLog']))
{
    //set the username and password variables from the form
    $username = $_POST['userLog'];
    $password = $_POST['passLog'];

    //create sql string to retrieve the string from the database table "users"
    $sql = "SELECT * FROM `users` WHERE userName = '". addslashes($username) ."' AND password = '". md5('$password')."'";
    $result = mysql_query($sql);
        if (mysql_num_rows($result)>0) {
            $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>";
        } else {
            $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
        }
        print($return);
}
于 2013-03-02T21:42:26.303 回答
0

mysql_query不返回 TRUE 或 FALSE。根据文档(http://php.net/manual/en/function.mysql-query.php),如果成功则返回资源,如果出现错误则返回 FALSE。您需要评估资源以查看它是否有效。

if(!empty($_POST['userLog']) && !empty($_POST['passLog']))
{
    //set the username and password variables from the form
    $username = $_POST['userLog'];
    $password = $_POST['passLog'];

    //create sql string to retrieve the string from the database table "users"
    $sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = md5('$password')";
    $result = mysql_query($sql);
    if ($result && $row = mysql_fetch_assoc($result)) {
        $return = "<font color=#008000><Center><b>**Successful Login**</b></Center></font>";
    } else {
        $return = "<font color=#ff0000><Center><b>**Failed Login**</b></Center></font>";
    }
    print($return);
}
于 2013-03-02T21:43:04.383 回答
0

正如我的评论中提到的,问题似乎是你的 sql 字符串。您将方法放入字符串中,而不是散列。所以改变

$sql = "SELECT * FROM `users` WHERE userName = '$username' AND password = md5('$password')";

$sql = "SELECT * FROM `users` WHERE userName ='$username' AND password = '".md5('$password')."'";

您的结果不会是 true 或 false,但由于 php 将任何非 0 的值视为 true,因此它将按原样工作。此外,强烈建议转义进入 sql 字符串的所有数据,以防止 sql 注入。另一个注意事项:mysql 已被弃用,所以现在是迁移到 mysqli 之类的好时机。

于 2013-03-02T21:47:37.147 回答