2014-02-28 436 views
0

我試圖做一個簡單的登錄,該ID和密碼的輸入由用戶與數據庫中的數據PHP ID和密碼驗證

//getting the inputs 
$checkid = $_POST["id"]; 

$checkpassword = md5($_POST["pass"]); 

//getting the id and password of the id and password of the inputs 
$query = "SELECT id, password FROM login WHERE id=$checkid AND password=$checkpassword"; 

$res = mysqli_query($link, $query); 

$nres = mysqli_num_rows($res); 

//$nres should be 0 if the user inputs the right id but the wrong password 
//or viceversa, the only way that it $nres!=0 is that both inputs match the db, right? 
if ($nres == 0) { 
    header('Location: http://localhost:8888/login/login_fail.php'); 
    else 
    header('Location: http://localhost:8888/profile/profile.php'); 
    exit(); 

這是行不通的比較,甚至如果我把正確的ID和數據庫上的密碼重定向到login_fail.php。 注意:它工作,如果我只是用他的ID和取出查詢「,密碼」「和密碼= $ checkpassword」。幫助

+2

添加引號'「SELECT ID,密碼從登錄WHERE ID = '$ checkid' 和密碼='$ checkpassword ''''和一個旁註:不要使用'md5',現在用作密碼存儲是不安全的。 –

+1

+1,並且還要注意,由於不對POST變量進行任何處理,因此您可以廣泛應對SQL注入攻擊。 – JBES

+0

你也可以修正你的條件陳述的支撐。 'if {...} else {...}' –

回答

2

添加引號的變量:

"SELECT id, password FROM login WHERE id='$checkid' AND password='$checkpassword'" 
             ^ ^   ^   ^

旁註:不要使用md5,它現在不安全的密碼存儲使用。

對於密碼存儲,請使用bcrypt或PHP的password()函數。

而看到this article also

在被別人評論也指出,使用mysqli_real_escape_string()

$checkid=mysqli_real_escape_string($link,$_POST['id']); 
0

嘗試查詢:

$query = "SELECT id, password FROM login WHERE id='".$checkid."' AND password='".$checkpassword."'"; 
+0

你需要'因爲你提交的數據庫類型是char或varchar,也不是int或float。 –