簡體   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