2012-06-15 34 views
0

我相信,我只是忽視的東西,我花了幾天這方面的工作,並不能似乎弄明白不工作。簡單,如果()如預期

前一頁我得到的用戶名和密碼在登錄後,

$username = mysql_escape_string($_POST['adminusername']); 
$password = mysql_escape_string($_POST['adminpassword']); 

,然後我去數據庫拉從數據庫中的用戶名和密碼,

$sql = "SELECT username, password FROM `weaponsadmin`"; 
$rows = $db->query($sql); while ($record = $db->fetch_array($rows)) { 

現在這裏是困惑我的一部分,如果我有以下的,不管我使用的用戶名或密碼,也不會允許我登錄,

if (($record[username]==$username) && ($record[password]==$adminpassword)){ 
    $_SESSION['loggedin'] = true; 
    $_SESSION['username'] = $adminusername; 
    header("Location: admin.php") ; 
} 
else { 
    header("Location: index.php?login=error") ; 
} 

但是如果我用下面,就會讓我登錄的用戶名是否正確,但它允許我輸入密碼的任何東西,它的工作原理,

$adminusername = $record[username]; 
$adminpassword = $record[password]; 

if (($adminusername==$username) && ($adminpassword==$adminpassword)) { 
    $_SESSION['loggedin'] = true; 
    $_SESSION['username'] = $adminusername; 
    header("Location: admin.php") ; 
} 
else { 
    header("Location: index.php?login=error") ; 
} 
在總結

所以出於某種原因在&&部分似乎並不正常工作,如果有人可以幫助我的代碼,讓我知道我的代碼可以更好的安全性,以及如何使這項工作正確地加以改進,感謝

+1

數組索引應該是整數或字符串,所以'$ record [username]'應該是'$ record ['username']'。沒有引號,PHP試圖找到'username' /'password'常量。文檔:http://php.net/manual/en/language.types.array.php – Jasper

+3

'$ adminpassword == $ adminpassword'永遠是真的.... – Wrikken

+0

@Wrikken yup這是問題,謝謝我知道它是很簡單的 –

回答

3

什麼是這裏$adminpassword==$adminpassword點:

我覺得應該是:

if (($adminusername==$username) && ($adminpassword==$password)){ 
+0

偉大的我告訴你這是一個非常簡單的事情,修復它,謝謝 –

+0

這也可以解釋爲什麼第一個例子從來沒有工作,因爲在這種情況下'$ adminpassword'從未設置 –

1

$record[username]應該是$record["username"](等等)。索引是字符串或INT

-1

不要爲字符串比較使用==。相反,使用strcmp()===來匹配字符串。

+1

爲什麼我們不應該使用'= ='用於字符串比較? – cypher

+0

它不準確,容易入侵。只需查看用戶評論所在的運營商部分的php在線文檔即可。詳細解釋了爲什麼它不安全。也googling php字符串比較會產生許多警告不會依靠==或===有很多原因。使用strcmp,preg_match或任何其他可靠的函數來比較字符串更可靠,特別是當它涉及管理員密碼和用戶名時。 – Tschallacka

+1

==和===是完全可靠的。沒有一個是真實的,並且使用preg_match進行字符串比較真是一個非常有意思的想法。 – cypher

0

可以使用===而不是==。閱讀this
strcmp()在這裏不是必須的。

+0

將使用身份運算符而不是平等真的解決他的問題嗎? – cypher

+0

這段代碼是問題:'$ adminpassword == $ adminpassword',就像之前說過的mgraph,而這個:'$ adminusername = $ record [username]; $ adminpassword = $ record [password];',但是更安全的使用'===' –

0

要添加到邁克爾的答案,爲什麼你不應該使用==字符串比較(希望這將有助於你在未來導航類似的困難)是,當你調用一個簡單的==對象(如一個字符串,或者除了int,double,float,char,long,short或boolean之外的任何其他語言),你真正比較的是每個對象的內存地址,也就是指針值。

這很有用,如果你想知道兩個變量是否引用同一個對象,但是如果你想知道兩個對象是否相同,那麼它就不那麼有用。因此,這是真的:

$string_a = $some_string; 
$string_b = $some_string; 
$string_a == $string_b; 

但這不是:

$string_a = getUserInput(); # user types in "hello" 
$string_b = getUserInput(); # user types in "hello" 
$string_a == $string_b; 

取決於你所處的語言,這可能是真實的,如果它在內存中存儲的字符串文字獨立於用戶的定義爲它們所連接的變量:

$string_a = "hello"; 
$string_b = "hello"; 
$string_a = $string_b; 

所以,除非你檢查,看看是否兩個對象其實都是同一個對象,而不是僅僅相同,使用那些在我之前的建議,一個函數的n來比較兩者。這樣的函數通常會下降到原始類型的級別,可以像使用==那樣進行比較,如果所有這些比較都返回true。

+1

爲什麼我們不應該在php中使用字符串比較'=='? – cypher

1

你正在使用數組錯誤。
您期望:$record[username]; //retrieve contains of key "username"
真正發生:

$record[username]; 
    /* 
     retrieves a key in the record array under the key which is a value of a 
     constant named "username" (if it's defined) and an empty string with 
     E_WARNING if it's not. 
    */ 



你需要無論是單或雙引號目錄名稱,例如$records['username']。 但是,您可以在字符串內使用未加引號的數組索引(並且這些將按預期工作) - >$someString = "Blahblahblah, ergo $record[username] is a donkey.";