簡體   English   中英

更改密碼PHP MySQL

[英]Changing a password 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";
       }
      ?>

目前的問題是,當我提交“提交”按鈕時,將我帶到此代碼的只讀頁面。

有什么建議么?

乍一看,我可以找出您犯的一些主要錯誤:

如果您正在比較,則應使用

$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部分。 請更正您的語法,然后重試...

您在這里犯了一個錯誤,其中include 'dbconfig.php';

應該像這樣include ('dbconfig.php');

u能1選擇一個表,並檢查密碼是否正確,如果正確的話一個更新密碼的.else顯示message.hey這是一個錯誤,如果是舊密碼只是更新密碼是匹配更新密碼...., 餅干世界

 $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 !!!";
}}

看完您的腳本后,我發現您沒有在密碼比較條件中使用'=='。

if($newpassword=$confirmnewpassword)

哪有錯 您接受了上面的birju shah的回答,指出了我想說的話。

除此之外,我發現您沒有使用任何加密方法,這是非常錯誤的。 任何人都可以從數據庫中破解您的密碼。

您應該使用Password_verify()和Password_hash()函數來加密和解密密碼。 如今,這些加密和解密被認為是最安全的。 您不應該使用md5加密,因為現在任何人都可以解密md5算法。

在這里,我做了一個教程,用PHP更改密碼代碼 我已經涵蓋了以上所有主題。 希望本教程對您有所幫助。

干杯,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM