php - 使用 php ajax 和 mysql 使用 secret 问题重置密码

标签 php javascript mysql ajax

enter image description here 我正在尝试编写一个脚本,允许用户在提供电子邮件地址并回答 secret 问题后重置密码。问题是我的 secret 问题脚本没有按预期工作。当用户回答 secret 问题时,它会在 ajax 的帮助下发布到 PHP 脚本,并返回 responseText,它应该根据返回的响应触发 ajax,但是当返回的文本满足其他一些条件时,这里的脚本总是显示 ajax 的其他条件。 ...任何帮助将不胜感激。预先感谢您的帮助..

重置密码步骤如下:

  1. 点击忘记密码
  2. 询问用户电子邮件地址
  3. 回答 secret 问题
  4. 在电子邮件中发送重置链接
  5. 点击电子邮件中发送的链接后,用户将被定向到密码重置页面,他们可以在其中创建新密码。

这是代码

<?php 

if(isset($_GET['e'])){
        // CONNECT TO THE DATABASE
        include_once("php_includes/connect_to_mysqli.php");
        // GATHER THE POSTED EMAIL INTO LOCAL VARIABLES AND SANITIZE
        $email = mysqli_real_escape_string($db_conx, $_GET['e']);

        $sql = "SELECT * FROM useroptions WHERE email='$email' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        $numrows = mysqli_num_rows($query);
        while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
            $id = $row["id"];
            $u = $row["username"];
            $q1 = $row["question"];
            $q2 = $row["question2"];
            $a1 = $row["answer"];
            $a2 = $row["answer2"];

                }
          if ($q1 == "" || $q2 == ""){
              header ("location: messages.php?emsg=forget&u=".$u);
                 exit();
              }
        }
?>
<?php
// AJAX CALLS THIS CODE TO EXECUTE
if(isset($_POST["pa1"])){
    include_once("php_includes/connect_to_mysqli.php");
    $e = mysqli_real_escape_string($db_conx, $_POST['em']);
    $pa1= $_POST['pa1'];
    $pa2= $_POST['pa2'];

    $sql = "SELECT * FROM useroptions WHERE email='$e' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        $numrows = mysqli_num_rows($query);
        while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
            $id = $row["id"];
            $a1 = $row["answer"];
            $a2 = $row["answer2"];
            }


    if ($pa1 == $a1 && $pa2 == $a2 && $e == $email ){
    $sql = "SELECT id, username FROM user WHERE email='$e' AND activated='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $numrows = mysqli_num_rows($query);
    if($numrows > 0){
        while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
            $id = $row["id"];
            $u = $row["username"];
        }
        $emailcut = substr($e, 0, 4);
        $randNum = rand(10000,99999);
        $tempPass = "$emailcut$randNum";
        $hashTempPass = md5($tempPass);
        $sql = "UPDATE useroptions SET temp_pass='$hashTempPass' WHERE username='$u' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        $to = "$e";
        $from = "auto_responder@yousite.com";
        $headers ="From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1 \n";
        $subject ="yoursite Temporary Password";
        $msg = '<h2>Hello '.$u.'</h2><p> Email with activation link</p>';
        if(mail($to,$subject,$msg,$headers)) {
            echo "success";
            exit();
        } else {
            echo "email_send_failed";
            exit();
        }
    }
    } else {
        echo "no_exist";
    }

    exit();
}
?>



    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Security Answer-</title>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="css/styles.css">
    <style type="text/css">
    #securityform{
        margin-top:24px;    
    }
    #securityform > div {
        margin-top: 12px;   
    }
    #securityform > input {
        width: 250px;
        padding: 3px;
        background: #F3F9DD;
    }
    #anssubmitbtn {
        font-size:15px;
        padding: 10px;
    }
    </style>
    <script src="js/main.js"></script>
    <script src="js/ajax.js"></script>
    <script>
    function emptyElement(x){
        _(x).innerHTML = "";
    }
    function ajaxObj( meth, url ) {
    var x = new XMLHttpRequest();
    x.open( meth, url, true );
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    return x;
}
function ajaxReturn(x){
    if(x.readyState == 4 && x.status == 200){
        return true;    
    }
}

    function forgotpasscon(){
        var em = _("email").value;
        var pa1 = _("ans1").value;
        var pa2 = _("ans2").value;
        if(em == "" || pa1 == "" || pa2 == ""){
            _("status").innerHTML = "Answer all security questions";
        } else {
            _("anssubmitbtn").style.display = "none";
            _("status").innerHTML = 'please wait ...';
            var ajax = ajaxObj("POST", "testconfirm.php");
            ajax.onreadystatechange = function() {
                if(ajaxReturn(ajax) == true) {
                    var response = ajax.responseText;
                    if(ajax.responseText == "success"){
                        _("securityform").innerHTML = '<h3>Step 2. Check your email inbox in a few minutes</h3><p>You can close this window or tab if you like.</p>';
                    } else if(ajax.responseText == "no_exist"){
                        _("status").innerHTML = "Sorry wrong answers";
                    } else if(ajax.responseText == "email_send_failed"){
                        _("status").innerHTML = "Mail function failed to execute";
                    } else {
                        _("status").innerHTML = "An unknown error occurred"+ajax.responseText;
                        _("anssubmitbtn").style.display = "block";
                    }
                }
            }
            ajax.send("em="+em+"&pa1="+pa1+"&pa2="+pa2);
        }
    }
    </script>

    </head>
    <body>
    <?php include_once("template_pageTop.php"); ?>
    <div id="pageMiddle">
        <h3>Step : 2</h3>
      <h4>Please answer the following security questions!!</h4>
      <form id="securityform" onsubmit="return false;">
        <div>Question 1:</div>
        <p><?php echo $q1; ?></p>
        <input name="ans1" id="ans1" type="text" onfocus="_('status').innerHTML='';" maxlength="100">
        <br/><br/>
        <div>Question 2:</div>
        <p><?php echo $q2; ?></p>
        <input name="ans2" id="ans2" type="text" onfocus="_('status').innerHTML='';" maxlength="100">
        <br /><br />
        <input name="email" id="email" type="hidden" value="<?php echo $email; ?>" />

        <button id="anssubmitbtn" onclick="forgotpasscon()">Submit</button> 
        <p id="status"></p>
      </form>
    </div>
    <?php include_once("template_pageBottom.php"); ?>
    </body> 
    </html> 

最佳答案

经排查,确定响应字符串需要裁剪:

ajax.onreadystatechange = function (evt) {
    if (ajaxReturn(ajax) == true) {
        var response = ajax.responseText.trim();
        //your if conditions here
    }
};

关于php - 使用 php ajax 和 mysql 使用 secret 问题重置密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15984822/

相关文章:

php - 在 Laravel 5.5 中使用 '&' 而不是 '&apm;' 查询的数组

javascript - 从 react 组件中提取输入参数

javascript - 有没有办法绕过 Javascript/jQuery 的本地访问同源策略?

javascript - 查找 HTML 数据属性是否存在于任何祖先元素中

php - 使用 php 和 mysql 每个正确答案的分数

mysql - 如何根据特定条件查询多个 MySQL 表?

php - 将数学放入 sql 查询中

php - 有没有办法在 php 中覆盖/重命名/重新映射函数?

php - MySQL 中的浮点值本身

mysql - 在 Jooq 中集成 Hikari Pool