2012-03-23 4 views
0

나는 PHP에서 비밀번호 변경 스크립트를 가지고 있는데, 사용자가 입력 한 이전 화면에서 변수를 가져 와서이를 mysql db와 비교합니다. 이전 암호가 입력 한 내용과 일치하지 않으면 오류가 발생하여 실패합니다. 이것은 내가 지금까지 가지고있는 코드입니다.하지만 변수를 비교하는 문자열은 작동하지 않을 것이지만 비교할 수 있도록 변환하는 방법을 알아야합니다. 아래는 문제의 페이지입니다.PHP에서 비밀번호 변경

암호는 현재 db의 Plain 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

코드는 어떻게 당신이 당신의 DB에서 암호를 저장하는 http://en.wikipedia.org/wiki/SQL_injection –

+0

하는 경향이있다? –

+1

좋습니다. 질문이 뭐야? 좋은 (그리고 답할만한) 질문에 대한 안내는 http://stackoverflow.com/questions/how-to-ask를 참조하십시오. – Hamish

답변

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

Thanks Sander 2 개의 새 암호가 일치하지 않으면 암호를 업데이트하기 직전에 상태가 "NOTOK"로 업데이트되지 않습니다. 문자열 길이와 일치하는 암호를 테스트했습니다. 내가 겪고있는 문제는 db에서 현재 암호를 가져온 다음 사용자가 입력 한 이전 암호와 비교하는 것입니다. 암호는 db의 일반 텍스트로 저장되지만 나중에 해시 또는 md5로 변경됩니다. 이것은 문제가 데이터베이스에서 가져온 무언가에 입력 된 것을 비교하는 것입니다. – mcj212

+0

@ mcj212 아, 기다려. 지금 배열과 문자열을 비교하고 있습니다. 비교할 배열의 요소를 지정해야합니다. '$ q1 [ 'field_name']'을 사용해야한다. 그래서, 여러분의 경우'$ q1 [ 'password']'. – Sander