更改密码PHP MySQL

时间:2013-03-19 20:43:12

标签: php mysql

我正在处理一段代码,在完成以下表单后更改数据库中的密码:

<html>
     <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Password Change</title>
     </head>
    <body>
    <h1>Change Password</h1>
   <form method="POST" action="password_change.php">
    <table>
    <tr>
   <td>Enter your UserName</td>
    <td><input type="username" size="10" name="username"></td>
    </tr>
    <tr>
    <td>Enter your existing password:</td>
    <td><input type="password" size="10" name="password"></td>
    </tr>
  <tr>
    <td>Enter your new password:</td>
    <td><input type="password" size="10" name="newpassword"></td>
    </tr>
    <tr>
   <td>Re-enter your new password:</td>
   <td><input type="password" size="10" name="confirmnewpassword"></td>
    </tr>
    </table>
    <p><input type="submit" value="Update Password">
    </form>
   <p><a href="home.php">Home</a>
   <p><a href="logout.php">Logout</a>
   </body>
    </html>  

PHP:

 <?php
session_start();
include 'dbconfig.php';

$username = $_POST['username'];
        $password = $_POST['password'];
        $newpassword = $_POST['newpassword'];
        $confirmnewpassword = $_POST['confirmnewpassword'];
        $result = mysql_query("SELECT password FROM user_info WHERE 
user_id='$username'");
        if(!$result)
        {
        echo "The username you entered does not exist";
        }
        else if($password!= mysql_result($result, 0))
        {
        echo "You entered an incorrect password";
        }
        if($newpassword=$confirmnewpassword)
        $sql=mysql_query("UPDATE user_info SET password='$newpassword' where 

 user_id='$username'");
        if($sql)
        {
        echo "Congratulations You have successfully changed your password";
        }
       else
        {
       echo "Passwords do not match";
       }
      ?>

我目前遇到的问题是,当我提交提交按钮时,会将我带到此代码的只读页面。

有什么建议吗?

4 个答案:

答案 0 :(得分:5)

初看起来我可以搞清楚你犯的几个重大错误:

如果您要进行比较,则应使用

$newpassword == $confirmnewpassword

而不是

$newpassword=$confirmnewpassword

其次当你使用if..elseif ...循环格式时,请

if (condition)
  {
  //code to be executed if condition is true;
  }
else if (condition)
  {
 // code to be executed if condition is true;
 }
else
  {
  //code to be executed if condition is false;
 } 

你显然错过了其中一个循环中的else部分。 请更正您的语法,然后重试......

答案 1 :(得分:0)

您在这里犯了一个错误 这个include 'dbconfig.php';

应该是这样的include ('dbconfig.php');

答案 2 :(得分:-1)

你可以先选择一个表并检查一个密码是否正确,如果更正密码.else会显示错误信息。如果旧密码匹配更新密码,则只需更新密码.... Cracker World

 $old_password=$_POST['old_password'];
$new_password=$_POST['new_password'];
$con_password=$_POST['con_password'];
$chg_pwd=mysql_query("select * from users where id='1'");
$chg_pwd1=mysql_fetch_array($chg_pwd);
$data_pwd=$chg_pwd1['password'];
if($data_pwd==$old_password){
if($new_password==$con_password){
$update_pwd=mysql_query("update users set password='$new_password' where id='1'");
$change_pwd_error="Update Sucessfully !!!";
}
else{
$change_pwd_error="Your new and Retype Password is not match !!!";
}
}
else
{
$change_pwd_error="Your old password is wrong !!!";
}}

答案 3 :(得分:-1)

看过你的脚本后,我发现你的密码比较条件没有使用'=='。

if($newpassword=$confirmnewpassword)

哪个错了。你接受了上面的birju shah的回答,指出了我想说的话。

除此之外,我发现你没有使用任何加密方法,这是非常错误的。任何人都可以从数据库中破解您的密码。

您应该使用Password_verify()和Password_hash()函数来加密和解密您的密码。这些加密和解密现在被认为是最安全的。您不应该使用md5加密,因为现在任何人都可以解密md5算法。

我在这里制作了一个教程Change password code in PHP。我已经涵盖了上述所有主题。我希望本教程能为您提供帮助。

干杯,