在PHP中更改密码
我在PHP中有一个更改密码脚本,我想做的是从用户输入的上一个屏幕中获取变量,然后将它们与mysql数据库进行比较。如果旧密码与他们输入的密码不匹配,我希望它失败并出现错误。这是我目前掌握的代码。。但我知道将字符串与变量进行比较是行不通的,但我需要知道如何转换它们,以便它们能够进行比较。下面是有问题的那一页 密码当前以明文形式存储在数据库中,但稍后将更改为md5。问题是如何将输入值与从数据库中提取的值进行比较在PHP中更改密码,php,passwords,Php,Passwords,我在PHP中有一个更改密码脚本,我想做的是从用户输入的上一个屏幕中获取变量,然后将它们与mysql数据库进行比较。如果旧密码与他们输入的密码不匹配,我希望它失败并出现错误。这是我目前掌握的代码。。但我知道将字符串与变量进行比较是行不通的,但我需要知道如何转换它们,以便它们能够进行比较。下面是有问题的那一页 密码当前以明文形式存储在数据库中,但稍后将更改为md5。问题是如何将输入值与从数据库中提取的值进行比较 <html> <head> <title>Passw
<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>
密码更改
首先,不建议在查询中直接使用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>
密码更改
你的代码容易出错,你是如何在数据库中存储密码的?这很好。你的问题是什么?查看有关询问良好(和可回答)问题的指南。感谢Sander,在更新密码之前,状态不会更新为“NOTOK”,除非两个新密码不匹配。我已经测试了字符串长度和匹配的密码。我遇到的问题是从数据库中提取当前密码,然后将其与用户输入的旧密码进行比较。密码现在以明文形式存储在数据库中,但稍后将更改为哈希或md5。这就是问题所在,将输入的内容与从数据库中提取的内容进行比较db@mcj212等等,我的坏朋友。您正在将数组与字符串进行比较。您应该指定要比较的数组元素。您应该使用$q1['field\u name']
来实现这一点。因此,在您的情况下,$q1['password']
。