PHP忘记了用户密码

时间:2016-04-11 18:09:31

标签: php

我有两个页面,其中有一个模式,底部有一个链接,用于“忘记密码”,然后应该从表单中携带用户的电子邮件并将其传递到我的第二页,然后构建并发送电子邮件使用随机生成的密码给用户。

的index.php

<!-- Modal -->
<div id="Login" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <h1> Sign up </h1>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>
      <div class="modal-body">
        <section class="container">
          <div id="login-form">
            <form method="post">
              <table align="center" width="30%" border="0">
                <tr>
                  <td><input type="text" class="form-control" name="email" placeholder="Your Email" required /></td>
                </tr>
                <tr>
                  <td><input type="password"  class="form-control" name="pass" placeholder="Your Password" required /></td>
                </tr>
                <tr>
                  <td><button type="submit" name="btn-login" class="btn btn-default">Sign In</button> 
                </tr>
              </table>
            </form>
          </div>
        </section>
      </div>
    <div class="modal-footer">
      <tr>
        <td><a href="register.php">Sign Up Here  |  </a></td>
        <td><a href="forgetpassword.php">Forgot Password</a></td>
      </tr>
    </div>
  </div>
</div>

forgetpassword.php

 <?php

    include('dbConnection.php');

    if (isset($_POST['email'])){ 

        $email = $_POST['email'];
        $new_password = substr(str_shuffle(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ),0,1) . substr(str_shuffle(aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789),0,15);
        $query = "SELECT `email` FROM `users` WHERE `email` ='".$email1."'";

        if (mysqli_num_rows ($result = mysqli_query($con, $query)) > 0){
            $row = mysqli_fetch_assoc($result);

            $email = $row['email'];
            $hashpass = hash ("sha256", $new_password); //hashing the password

            $query1 = "UPDATE `users` SET `pass` = '".$hashpass."' WHERE `email` = '".$email."'";

            $result1 = mysqli_query($con, $query1);

            if ($email == ""){
                echo "<script type ='text/javascript'>alert('Re-type your email address and try again!');</script>";
            }

            $to = $email;
            $subject = "Change your password";
            $message = "Below is your new password \r\n\r\n " 
            .$newpassword. "Thanks";
            $headers = "From: saracassells@gmail.com";
            $mail = mail($to, $subject, $message, $headers);
        }
    }

    ?>

我的错误似乎与行if (isset($_POST['email']))一致,就像在显示回显之前放置回声一样,但是之后没有。任何想法?

2 个答案:

答案 0 :(得分:0)

我认为你错过了str_shuffle()调用的引号。

$new_password = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),0,1) . substr(str_shuffle('aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789'),0,15);

http://php.net/manual/en/function.str-shuffle.php

答案 1 :(得分:0)

看起来您的Forgot Password链接只是一个href链接。所以它只是将用户重定向到该页面。但是,由于您正在寻找要在后期数据中设置的email属性,因此在该条件块中不会进行任何操作。

可能有很多方法可以做到这一点。您可以在forgotpassword.php页面上提示用户输入他们的电子邮件地址,然后在再次提交到同一页面的表单中输入该输入,因此可以像您期望的那样在发布数据中包含电子邮件。