首页 文章

用户使用php和mysql数据库登录

提问于
浏览
0

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

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,因为它已被折旧 .

  • 0

    首先,保护您的代码免受SQL injections的影响 .

    然后,确保使用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);
    }
    
  • 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);
    }
    
  • 0

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

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

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

    你的结果不会是真或假,但由于php将任何值都不是0视为真,它将按原样运行 . 此外,强烈建议转义进入sql字符串的所有数据以防止sql注入 . 另一个注意事项:mysql正在被弃用,所以现在是转向mysqli之类的好时机 .

相关问题