2013-03-02 46 views
0

我是mysql和php的新手。用戶使用php和mysql數據庫登錄

一直在爲用戶創建一個帶有表的數據庫。

我已經成功地將用戶添加到數據庫,他們的密碼與md5(是的,我知道它不安全),它不會在線啓動。

我的問題是,如何根據用戶名和密碼登錄用戶。

這裏是我的代碼

我的邏輯是taht查詢運行後,它會返回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); 
} 
+1

看起來像md5(密碼)將成爲qry字符串的一部分。嘗試將其更改爲'$ sql =「SELECT * FROM \'users \'WHERE userName ='$ username'AND password ='」.md5('$ password')。「'」;' – SchautDollar 2013-03-02 21:38:39

+0

快速提示使用phpass - > http://www.openwall.com/phpass/ – Espen 2013-03-02 21:43:18

回答

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 qauery的值,並且它們是正確的。然而,我仍然得到一個失敗的登錄,並且我得到一個錯誤mysql_num_rows()期望參數1是資源,布爾值 – user1050632 2013-03-02 21:58:05

+0

您是否更新了上面提到的$ sql變量? – nine7ySix 2013-03-02 22:01:09

+0

是的,我做了,並且我打印了新的passwordHash值,它匹配了mysql表中的內容 – user1050632 2013-03-02 22:02:54

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之類的好時機。