2012-11-14 138 views
0

所以我把一個非常粗略的登錄表單使用PHP和MySQL數據庫,我有它設置(或因此我認爲),以回重定向到登錄頁面與「loginFailed =真&原因=密碼」」我。我試圖讓它重定向回登錄,並顯示一個不正確的密碼信息,但它只是重定向到主索引頁不正確的密碼重定向到不正確的頁面?

我在這裏做錯了什麼?當然, 。現有的代碼,由於我缺乏的編碼知識,但它並重定向之前工作打算了一下

下面是代碼:

passwordcheck .PHP

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// username and password sent from form 
$password=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection) 
$password = stripslashes($password); 
$password = mysql_real_escape_string($password); 
$sql="SELECT * FROM $tbl_name WHERE password='$password'"; 
$result=mysql_query($sql); 

// Mysql_num_row is counting table row 
$count=mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count==1){ 

// Register $myusername, $mypassword and redirect to file "login_success.php" 
session_register("password"); 
header("location:admin.html"); 
} 

else { 
die(header("location:login.html?loginFailed=true&reason=password")); 
} 
?> 

這裏是在登錄頁面的密碼字段:

<span class="add-on"><i class="icon-list-alt"></i></span> 
<input type="password" id="inputIcon" class="span4" name='password' id='password' maxlength="50" placeholder="<?php $reasons = array("password" => "Yo shitbird, wrong password."); if ($_GET["loginFailed"]) echo $reasons[$_GET["reason"]]; ?>" /> 
</div> 
+0

所以沒有用戶名場,只是一個密碼?這意味着每個用戶都需要一個唯一的密碼;如果你爲他們生成密碼,這可能會有效,但最好還是有一個用戶名。 –

回答

0

嘗試移動header()命令出die()通話:

else { 
    header("location:login.html?loginFailed=true&reason=password"); 
    die(); 
} 

還有許多其他的潛在這個代碼的問題,我建議閱讀關於這個主題的一些教程,there are plenty out there;儘管要小心,但有許多低質量的PHP教程可能會教你危險的做法。瞭解關於PHP security的更多信息非常重要,特別是如果此代碼將位於可公開訪問的Web服務器上。

其中的一個問題是,你存儲明文密碼的事實。永遠不要以明文形式存儲密碼,它們應該被醃製並與a secure hashing algorithm一起存儲。 PHPass是一個很好的工具來幫助你做到這一點。

+0

我想到了加密密碼,但實際上它只是一個側邊欄的個人網站小編輯部分。雖然......我可能想要考慮加密只是爲了構建未來項目的技能。 – Cassette