繁体   English   中英

用php和mysql数据库登录用户

[英]user login with php and mysql database

我是mysql和php的新手。

一直在为用户创建带有表的数据库。

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

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

这是我的代码

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

如果为true,则显示成功登录,否则不成功。

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

我检查了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);
}

我不确定您的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代替MySQLi。

首先,保护您的代码免遭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);
}

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);
}

如我的评论中所述,问题似乎是您的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之类的好时机。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM