如何使用PHP中的$ _GET从URL获取数据

时间:2016-07-11 04:12:42

标签: php mysqli get

我正在尝试实现密码重置功能。这是我的代码

的index.php

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method = "POST">

        <input type = "email" name = "recover_email" placeholder = "Enter Your Email Address" required/>                    
        <input type = "submit" name= "recover" value = "Recover"/>

</form>

<?php     

    if(isset($_POST['recover']) && $_POST['recover'] == 'Recover'){
        $email = $_POST['recover_email'];

        $sql = $db->prepare("SELECT * from registered_users where email = ?");
        $sql->bind_param("s",$email);
        $sql->execute();
        $sql = $sql->get_result();

        if(($getRowCount = $sql->num_rows) == 1){ 
                $rowName = $sql->fetch_assoc();

                $code = rand(1000, 1000000);

        mail();

$updateCode = $db->prepare("UPDATE registered_users SET passwordResetCode = ? where email=?");
                 $updateCode->bind_param("is",$code,$email);
                 $updateCode->execute();
                 $updateCodeResult = $updateCode->get_result();
            }   

    }else if(!(($getRowCount = $sql->num_rows) == 1) && $email != ''){
       //do something
      }
    }

    ?>

正在发送电子邮件正文:

http://www.website.com/passwordreset.php?code=$code&email=$email

邮件,sql更新和链接详细信息正在正常发送。

现在,我尝试更新密码,点击下一页上的链接。

passwordreset.php

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method = "POST">

     <table cellpadding = "5%" style="width:70%">
        <tr>
          <td>
            <input type = "password" id="newPassword" name = "newPassword" placeholder = "New Password" minlength="8" required />
          </td>
        </tr> 

        <tr>
          <td>
            <input  type = "password" id= "confirmnewPassword" name = "confirmnewPassword" placeholder = "Confirm New Password" minlength="8" required />
          </td>                    
        </tr>

        <tr>
          <td>
            <input type = "submit" name= "setNewPassword" value = "Set Password"/> 
          </td>
        </tr>

    </table>
   </form>

<?php

    if(isset($_GET['code'])){

        $getEmail = $_GET['email'];
        $getCode = $_GET['code'];

        if(isset($_POST['setNewPassword']) && $_POST['setNewPassword'] == 'Set Password'){

        $newPassword = $_POST['newPassword'];
        $confirmNewPassword = $_POST['confirmnewPassword'];         

        if($newPassword == $confirmNewPassword){

        $updatePassword = $db->prepare("UPDATE registered_users SET password = ?, re_password = ? where email = ? and passwordResetCode = ?");
        $updatePassword->bind_param("sssi",$newPassword,$confirmNewPassword,$getEmail,$getCode);
        $updatePassword->execute();     

        }else if($newPassword != $confirmNewPassword){
        }
        }           
    }   
    ?>

如果我从此代码中删除所有$ _GET功能,它可以正常工作,但似乎$ _GET根本无法从网址中获取我的电子邮件和代码。

1 个答案:

答案 0 :(得分:0)

您的脚本执行以下操作。 1.使用参数重定向到密码重置页面。 2.获取帖子重置密码(但没有得到参数) 您必须存储get,直到用户发布新密码

if(isset($_GET["code"])){
session_start();
$_SESSION["code"]=$_GET["code"];
}

当用户发布新密码时,请访问它。

session_start();
$code=$_SESSION["code"];