在php中添加几行代码

nr7wwzry  于 2021-06-25  发布在  Mysql
关注(0)|答案(3)|浏览(263)

错误我已经写了一个php程序,其中采取用户新的和旧的密码我的代码运行良好,但现在我不得不在我的php程序几行代码。这是我的php代码,我写的,我想添加几行代码,但当我写新的代码在它的工作,但它显示新的错误,我必须写的代码是警告用户,用户“新密码应与旧密码不同”。当用户进入与旧密码网页相同的新密码提交按钮时,此代码警告用户。
这是我的php程序:

<?php 
session_start();

// if ($_SESSION['user_name'] != "") 
// {
//     header("location:account.php");
// }

include('connection.php'); 

// header("Refresh: 20; URL=welcome.php");
// header("Refresh: 20; URL=http://www.stackoverflow.com/");

if(isset($_POST['submit']))
{
    $old_password = $_POST['old_password'];
    $new_password = $_POST['new_password'];

    $query = $con->prepare("select password from tbl_users WHERE id = :user_id");
    $query->bindParam(':user_id', $_SESSION['id']); 
    $query->setFetchMode(PDO::FETCH_ASSOC);
    $query->execute();
    $fetch = $query->fetch();

    $old_pass = $fetch['password']; 

    if($old_password == $old_pass){
        $stmt = $con->prepare("UPDATE tbl_users SET password = (:pass) WHERE id = :user_id");
        $stmt->bindParam(':pass', $new_password, PDO::PARAM_STR);       
        $stmt->bindParam(':user_id', $_SESSION['id']);    
        // $stmt->execute(); 

        $stmt->execute();
        header("location:account.php");
    }
    else
    { 
        echo "<script>alert('Wrong password! Enter your valid old password')</script>";
    }
}
?>

html代码:

<!DOCTYPE html>
<html>
<head>
  <title>project</title>
  <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="registration.css">
  <script type="text/javascript" src="js/bootstrap.min.js"></script>
  <script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
<header><h1>Change Password</h1></header>
<form method="post" action="renew.php">
<br />
<input type="password" id="pwd2" placeholder="Enter your old password" name="old_password" required />
<br />

<input type="password" id="pwd1" placeholder="Enter your new password" name="new_password" required />
<center>
<!-- <div class="form-group"> -->
      <div id="setPasswordMessage" style="display: none;"></div>
<!-- </div> -->
</center>
<br />

<div class="buttons">
<input type="submit" disabled="submit" class="btn"  name="submit" value="Save">
</div>
<br />
</form>
<footer><h3>Copyright &copy; vu.edu.pk (S1701F607E)</h3></footer>

<script type="text/javascript">
$(document).ready(function() {

  var pwd1    = $('#pwd1'); //id of first password field
  var pwd2    = $('#pwd2'); //id of second password field
  var pwdIdSet  = $('#setPasswordMessage'); //id of indicator element

  setCheckPasswordStrength(pwd1,pwd2,pwdIdSet); //call password check function

});

function setCheckPasswordStrength(pwd1, pwd2, pwdIdSet)
{
  /*=========== Start: Set Password Cretria Regular Expression ===================*/

  //Password must contain 5 or more characters
  var lowPassword = /(?=.{5,}).*/;  

  //Password must contain at least one digit and lower case letters .
  var mediumPassword = /^(?=\S*?[a-z])(?=\S*?[0-9])\S{5,}$/;

  //Password must contain at least one digit, one upper case letter and one lower case letter.
  var averagePassword = /^(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])\S{5,}$/; 

  //Password must contain at least one digit, one upper case letter and one lower case letter.
  var strongPassword = /^(?=\S*?[A-Z])(?=\S*?[a-z])(?=\S*?[0-9])(?=\S*?[^\w\*])\S{5,}$/; 

  /*=========== End: Set Password Cretria Regular Expression ===================*/

// test() method is used to test match in a string whether the value is matched in a string or not.

  $(pwd1).on('keyup', function(e) {
    var len = $('#pwd1').val().length;
    document.getElementById("setPasswordMessage").style.display="block";
    if(strongPassword.test(pwd1.val()))
    {
      pwdIdSet.removeClass().addClass('strongPassword').html("Strong! Please use this password!").css("display","block");
      $(':input[type="submit"]').prop('disabled', false);
    } 
    else if(averagePassword.test(pwd1.val()))
    {
      pwdIdSet.removeClass().addClass('averagePassword').html("Average! Tips: Enter special characters to make even stronger").css("display","block");
      $(':input[type="submit"]').prop('disabled', true);
    } 
    else if(mediumPassword.test(pwd1.val()))
    {
      pwdIdSet.removeClass().addClass('mediumPassword').html("Good! Tips: Enter uppercase letter to make strong").css("display","block");
      $(':input[type="submit"]').prop('disabled', true);
    }
    else if(lowPassword.test(pwd1.val()))
      {
      pwdIdSet.removeClass().addClass('stilllowPassword').html("Still Weak! Tips: Enter digits to make good password").css("display","block");
      $(':input[type="submit"]').prop('disabled', true);
      }

      else if(len < 1)
      {
        pwdIdSet.removeClass('lowPassword');
        $('#setPasswordMessage').css("display","none");
        $(':input[type="submit"]').prop('disabled', true);
      }

    else 
    {
      pwdIdSet.removeClass().addClass('lowPassword').html("Very Weak! Please use 5 or more chars password)").css("display","block");
      $(':input[type="submit"]').prop('disabled', true);
    }
  });

  // $(pwd2).on('keyup', function(e) {

  //  if(pwd1.val() !== pwd2.val()) 
  //  {
  //    pwdIdSet.removeClass().addClass('lowPassword').html("Passwords do not match!"); 
  //  }else{
  //    pwdIdSet.removeClass().addClass('goodpass').html("Passwords match!"); 
  //  }

  // });
}
</script>
</body>
</html>

我必须在php代码中添加这个代码,但是在什么地方以及如何添加。

if($old_password == $new_password)
{ 
  echo "<script>alert('New password should be different with old password')</script>";
}
5uzkadbs

5uzkadbs1#

在进行查询之前先检查一下。

<?php 
session_start();

// if ($_SESSION['user_name'] != "") 
// {
//     header("location:account.php");
// }

include('connection.php'); 

// header("Refresh: 20; URL=welcome.php");
// header("Refresh: 20; URL=http://www.stackoverflow.com/");

if(isset($_POST['submit']))
{
    $old_password = $_POST['old_password'];
    $new_password = $_POST['new_password'];

    if ($old_password == $new_password) {
        echo "<script>alert('New password should be different with old password')</script>";
    } else {
        $query = $con->prepare("select password from tbl_users WHERE id = :user_id");
        $query->bindParam(':user_id', $_SESSION['id']); 
        $query->setFetchMode(PDO::FETCH_ASSOC);
        $query->execute();
        $fetch = $query->fetch();

        $old_pass = $fetch['password']; 

        if($old_password == $old_pass){
            $stmt = $con->prepare("UPDATE tbl_users SET password = (:pass) WHERE id = :user_id");
            $stmt->bindParam(':pass', $new_password, PDO::PARAM_STR);       
            $stmt->bindParam(':user_id', $_SESSION['id']);    
            // $stmt->execute(); 

            $stmt->execute();
            header("location:account.php");
        }
        else
        { 
            echo "<script>alert('Wrong password! Enter your valid old password')</script>";
        }
    }
}
?>
5fjcxozz

5fjcxozz2#

如果在任何输出之后发送头,php将抛出错误。因此,如果您以前有任何输出 header("location:account.php"); ,将发生错误。尝试通过js设置新位置:

// header("location:account.php");
echo "<script>document.location.href = 'account.php';</script>";

完整代码如下所示:

if($old_password == $old_pass){
    if($old_password == $new_password)
    { 
      echo "<script>alert('New password should be different with old password')</script>";
    } else {
        $stmt = $con->prepare("UPDATE tbl_users SET password = (:pass) WHERE id = :user_id");
        $stmt->bindParam(':pass', $new_password, PDO::PARAM_STR);       
        $stmt->bindParam(':user_id', $_SESSION['id']);    
        // $stmt->execute(); 

        $stmt->execute();
        // header("location:account.php");
        echo "<script>document.location.href = 'account.php';</script>";
    }
}
else....
rbpvctlc

rbpvctlc3#

你应该在设置变量之后再检查一下 $old_password 以及 $new_password 在执行任何数据库查询之前(如果新旧密码相同,则不希望执行这些查询):

if(isset($_POST['submit']))
{
    $old_password = $_POST['old_password'];
    $new_password = $_POST['new_password'];

    if($old_password == $new_password)
    { 
        echo "<script>alert('New password should be different with old password')</script>";
    }
    else
    {

        $query = $con->prepare("select password from tbl_users WHERE id = :user_id");
        $query->bindParam(':user_id', $_SESSION['id']); 
        $query->setFetchMode(PDO::FETCH_ASSOC);
        $query->execute();
        $fetch = $query->fetch();

        $old_pass = $fetch['password']; 

        if($old_password == $old_pass){
            $stmt = $con->prepare("UPDATE tbl_users SET password = (:pass) WHERE id = :user_id");
            $stmt->bindParam(':pass', $new_password, PDO::PARAM_STR);       
            $stmt->bindParam(':user_id', $_SESSION['id']);    
            // $stmt->execute(); 

            $stmt->execute();
            header("location:account.php");
        }
        else
        { 
            echo "<script>alert('Wrong password! Enter your valid old password')</script>";
        }
    }
}

虽然它与编码无关,但我只想指出,您的错误信息不是正确的英语。有些东西是“不同于”另一些东西,而不是“不同于”。使用此选项:
新密码必须与旧密码不同

相关问题