2012-03-23 117 views
0

我在PHP中更改密碼腳本,我想要做的是從前一個屏幕中輸入用戶輸入的變量,然後將它們與MySQL數據庫進行比較。如果舊密碼與他們放入的不匹配,我希望它失敗並出現錯誤。這是我到目前爲止的代碼..但我知道比較一個字符串與變量不會工作,但需要知道如何轉換它們,以便他們可以比較。以下是有問題的頁面。在PHP中更改密碼

密碼當前存儲在數據庫的普通txt中,但稍後將更改爲md5。問題是如何將輸入值與從db中拉出的值進行比較?

<html> 
<head> 
<title>Password Change</title> 
</head> 
<body> 

<?php 

mysql_connect("localhost", "kb1", "BajyXhbRAWSVKPsA") or die(mysql_error()); 
mysql_select_db("kb1") or die(mysql_error()); 

    $todo=mysql_real_escape_string($_POST['todo']); 
    $username=mysql_real_escape_string($_POST['userid']); 
    $password=mysql_real_escape_string($_POST['password']); 
    $password2=mysql_real_escape_string($_POST['password2']); 
    $oldpass=mysql_real_escape_string($_POST['oldpass']); 

///////////////////////// 

if(isset($todo) and $todo == "change-password"){ 
//Setting flags for checking 
$status = "OK"; 
$msg=""; 

//MYSQL query to pull the current password from the database and store it in $q1 

$results = mysql_query("SELECT password FROM kb_users WHERE username = '$username'") or    die(mysql_error()); 
$q1 = mysql_fetch_array($results); 
//print_r($q1) 


//changing the string $oldpass to using the str_split which converts a string to an  array. 

//$oldpass1 = str_split($oldpass,10); 

if(!$q1) 

    { 
     echo "The username <b>$username</b> does not exist in the database. Please   click the retry button to attempt changing the password again. <BR><BR><font face='Verdana'  size='2' color=red>$msg</font><br><center><input type='button' value='Retry'  onClick='history.go(-1)'></center>"; die(); 
    } 

if ($oldpass == $q1){ 

$msg = $msg. "The provided password <b>$oldpass</b> is not the same as what is in the  database. Please click the retry button to attempt changing the password again.<BR><br>"; 

$status = "NOTOK";} 
/* 
if ($q1 <> $oldpass1) {  
$msg = $msg. "The provided password <b>$oldpass</b> is not the same as what is in the  database. Please click the retry button to attempt changing the password again.<BR><br>"; 
$status = "NOTOK"; } 
*/ 

if (strlen($password) < 3 or strlen($password) > 10){ 
$msg=$msg. "Your new password must be more than 3 char legth and a maximum 10 char  length<BR><BR>"; 
$status= "NOTOK";}     

if ($password <> $password2){ 
$msg=$msg. "Both passwords are not matching<BR>"; 
$status= "NOTOK";}     


if($status<>"OK") 
    { 
     echo "<font face='Verdana' size='2' color=black>$msg</font><br><center> <input type='button' value='Retry' onClick='history.go(-1)'></center>"; 
    } 
     else { 
     // if all validations are passed. 

      if (mysql_query("UPDATE kb_users SET password='$password' where   username='$username'") or die(mysql_error())); 

       { 
        echo "<font face='Verdana' size='2' ><center>Thanks  <br> Your password has been changed successfully. Please keep changing your password for  better security</font></center>"; 
       } 
      } 
     } 


?>  
</body> 
</html> 
+4

您的代碼很容易出現http://en.wikipedia.org/wiki/SQL_injection – 2012-03-23 23:49:20

+0

你是如何存儲在你的數據庫密碼? – 2012-03-23 23:50:16

+1

很好。你有什麼問題?查看http://stackoverflow.com/questions/how-to-ask獲取有關問好(和可回答)問題的指南。 – Hamish 2012-03-23 23:57:36

回答

1

首先,建議不要直接在查詢中使用POST數據。你最好先逃避這些數據,以避免注射。

另外,我認爲你使用if的方式不是最好的方法。我認爲不需要狀態變量。這是肯定的。 $status在測試它的值之前設置爲NOTOK。所以它永遠是NOTOK,這會導致你的腳本永遠不會更新任何密碼。

我改變了你的測試結構,在我看來,更好的一個。仔細看看你想測試什麼,因爲現在你的測試都混在一起了。

<html> 
<head> 
    <title>Password Change</title> 
</head> 

<body> 

    <?php 
     // MySQL connection details 

     $todo=mysql_real_escape_string($_POST['todo']); 
     $username=mysql_real_escape_string($_POST['userid']); 
     $password=mysql_real_escape_string($_POST['password']); 
     $password2=mysql_real_escape_string($_POST['password2']); 
     $oldpass=mysql_real_escape_string($_POST['oldpass']); 

     if(isset($todo) and $todo == "change-password"){ 

      $results = mysql_query("SELECT password FROM kb_users WHERE username = '$username'") or    die(mysql_error()); 
      $q1 = mysql_fetch_array($results); 

      if (!$q1) { 
       // The user does not exist in the database. 
      } 

      if ($oldpass == $q1) { 
       // The current password matches the input from the oldpass field. 

       if (strlen($password) > 3 or strlen($password) < 10) { 
        // Password meets requirements 
        if ($password == $password2) { 
         //Passwords match, update the password in the database 
        } 
        else { 
         // The new passwords do not match. 
        } 
       } 
       else { 
        // Password is too short/long 
       } 

      } 
     } 
    ?> 
</body> 

+0

感謝Sander更新密碼之前,狀態不會更新爲「NOTOK」,除非2個新密碼不匹配。我測試了字符串長度和匹配的密碼。我遇到的問題是從數據庫中提取當前密碼,然後將其與用戶輸入的舊密碼進行比較。密碼現在以純文本形式存儲在db中,但稍後將更改爲散列或md5。 所以這就是問題在哪裏比較輸入的東西從db – mcj212 2012-03-24 16:13:42

+0

@ mcj212拉啊等等,我的壞。你現在將一個數組與一個字符串進行比較。您應該指定要比較的數組的哪個元素。你應該使用'$ q1 ['field_name']'。所以,在你的情況下'$ q1 ['password']'。 – Sander 2012-03-24 18:00:48