2010-02-26 22 views
0

用戶名行完好顯示,但密碼行拒絕通過。mysql_fetch assoc拒絕返回密碼行。需要幫助

我在這裏不知所措。

有誰知道解決方案是什麼?

這裏是我的代碼::

<?php 

//Mass include file 
include ("includes/mass.php"); 

//This is the login script 
//Grabbing the login values and storing them 

$username = $_POST['username']; 
$password = $_POST['password']; 
$submit = $_POST['submit']; 

if (isset($submit)) 
{ 
    if (strlen($username)<2) // put || ($username==(same as value on the database) 
    { 
     echo ("<br>You must enter a longer username</br>"); 
    } 
    elseif (strlen($password)<=6) 
    { 
     echo ("<br>You must enter a longer password<br>"); 
    } 
    else 
    { 
     $sql = "SELECT * FROM user WHERE username = '$username'"; 
     $query = mysql_query($sql); 
     $numrows = mysql_num_rows($query); 

     if ($numrows != 0) 
     { 
      while ($row = mysql_fetch_assoc($query)) 
       $dbusername = $row['username']; 
      $dbpassword = $row['password'];   

      if ($dbusername == $username && $dbpassword == $password) 
      { 
       echo "your in!"; 
      } 
      else 
      { 
       echo "Wrong info"; 
      } 
     } 
     else 
     { 
      die ("That username doesnt exist"); 
     } 
    } 
} 

?> 
+3

您不應該在數據庫中保留非哈希密碼,而應該使用預準備語句。上面的代碼完全容易受到SQL注入攻擊。 – 2010-02-26 13:11:16

+0

是的,我知道。我這樣做最初是爲了測試。當我獲得正確的功能時,我專注於安全性。 雖然準備好的陳述意味着什麼? – Tapha 2010-02-26 13:41:19

+0

由於您是新手,因此我想告訴您,您可以通過點擊答案旁邊的「正確標記」來接受最能幫助您的答案。 – gameover 2010-02-26 13:43:36

回答

4

你有一段時間後,丟失的圓括號。

兩個

$dbusername = $row['username']; 
$dbpassword = $row['password'];  

應該是while循環這不是在殼體內。

你需要做的是這樣的:

while ($row = mysql_fetch_assoc($query)) { 

    $dbusername = $row['username']; 
    $dbpassword = $row['password'];   

    if ($dbusername == $username && $dbpassword == $password) { 
     echo "your in!"; 
    } 
.... 

添加到什麼馬修說:

你不應該像顯示信息「的用戶名犯規存在」給用戶。這爲希望入侵的黑客提供了有價值的信息。如果用戶提供了錯誤的用戶名和/或錯誤的密碼,則應顯示「無效的用戶名或密碼」。

+0

謝謝!它現在有效 – Tapha 2010-02-26 13:37:36

0

如果用戶名不唯一,您的腳本將失敗。這可能是這種情況嗎?

除此之外,存儲密碼以純文本格式,而不是清理用戶輸入:非常糟糕的想法...

編輯:順便說一句,爲什麼難道你不檢查一個有效的用戶名/密碼組合在查詢本身:

... WHERE username='" . mysql_real_escape_string($username) . "' 
    AND password='" . some_hashing_function($password) . "'"; 

另一個編輯:你必須在所發生的事情仔細看;我的初始答案(非唯一用戶ID)可能是一個問題,但@ codaddict的答案已經可以解決您的問題。

基本上,就是你的發言,猶如沒有花括號是這樣的:

while ($row = mysql_fetch_assoc($query)) 
    $dbusername = $row['username']; 
    $dbpassword = $row['password'];   
    .... 

是PHP中的一樣:

while ($row = mysql_fetch_assoc($query)) 
{ 
    $dbusername = $row['username']; 
} 
$dbpassword = $row['password']; 
... 

所以,當你要填寫你的$時間dbpassword,$行已經是空的。

+0

嗨Jeroen 感謝您的意見。此腳本用於登錄。未註冊。這就是爲什麼我沒有包含哈希。 – Tapha 2010-02-26 13:36:42

+0

Tapha,那沒關係,當你在註冊時存儲密碼的時候,你應該存儲一個散列並且在登錄時匹配,你還需要散列進行比較。如果你有一個數據庫充滿純文本密碼,很容易一次對所有密碼進行哈希處理;如果有必要,請在新字段中輸入,並在所有內容正常工作時刪除純文本密碼。 (打頭) – jeroen 2010-02-26 13:47:34

+0

謝謝。我犯這些錯誤。 :)我是一個新手。 – Tapha 2010-02-26 13:50:09

0
 
I like the suggestion about the {} that normally encompasses the code that occurs in a while loop. Id replace 

while ($row = mysql_fetch_assoc($query)) 
       $dbusername = $row['username']; 
      $dbpassword = $row['password']; 

with 

while ($row = mysql_fetch_assoc($query)) 
{ 
    $dbusername = $row['username']; 
    $dbpassword = $row['password']; 
} 
// this lets the loop finish so $dbusername and $dbpassword are potentially set 
// so you can contine with your if statements 

Another thing to check is your html - check your password field looks something 
like <input type="password" name="password" /> 

You could always try echo $password; immediately after $_POST['password'] to see 
if it existed in the first place. Then echo any of your variables after they have 
been set to see if they exist. 
+1

這個人沒有問,但從他的代碼看來,他可能並不知道這些危險的錯誤。 – gameover 2010-02-26 13:36:44