【问题标题】:Add few lines of code in php在php中添加几行代码
【发布时间】:2018-01-10 20:54:32
【问题描述】:

Errors 我编写了一个 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>";
}

【问题讨论】:

  • 对不起,有什么错误?你有什么困难?
  • 首先,你不应该在数据库中存储明文密码,你应该使用password_hash()和password_verify()。
  • 你说话就像别人写了上面的代码
  • 我正在发布一张图片,我的问题请稍候
  • 错误是未定义的变量:C 中的 old_password:------------ 第 42 行,未定义的变量:C 中的新密码:---------- -- 在第 42 行

标签: php mysql server phpmyadmin


【解决方案1】:

在进行查询之前进行检查。

<?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>";
        }
    }
}
?>

【讨论】:

  • 谢谢兄弟,它有效非常感谢你,上帝保佑你
【解决方案2】:

如果在任何输出之后发送标头,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....

【讨论】:

    【解决方案3】:

    您应该在设置变量$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>";
            }
        }
    }
    

    虽然它与编码无关,但我只是想指出您的错误信息不是正确的英文。某物与某物“不同”,而不是“与”不同。使用这个:

    您的新密码必须与旧密码不同

    【讨论】:

      猜你喜欢
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 2012-08-08
      • 1970-01-01
      • 2013-08-19
      相关资源
      最近更新 更多