나는 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>
코드는 어떻게 당신이 당신의 DB에서 암호를 저장하는 http://en.wikipedia.org/wiki/SQL_injection –
하는 경향이있다? –
좋습니다. 질문이 뭐야? 좋은 (그리고 답할만한) 질문에 대한 안내는 http://stackoverflow.com/questions/how-to-ask를 참조하십시오. – Hamish