存储$ _GET并在POST请求中再次使用它

时间:2018-02-28 06:09:33

标签: php post get

我正在尝试将$_GET存储在变量中并在POST请求中重复使用它们,但问题是,只要发送POST请求,URL就会变为空,并且没有任何内容存储在变量中或者删除存储在变量中的数据,因为URL中没有任何内容

success.php?email=16564017@gmail.com&token=M3XK5HeCZy当前网址

发送POST请求后,URL为success.php,不存储任何内容,

我尝试根据此方法更新用户密码

我已经用$_SESSION尝试了这个,但无法理解,我目前的工作

if(!$_GET['email'] && !$_GET['token']) {
    header("Location: register.php");
}
else {


$arrayCookie = array('email' => $_GET['email'] , 'token' => $_GET['token']);
$json = json_encode($arrayCookie);
setcookie('data',$json,time()+(8400));

$cookie = $_COOKIE['data'];
$cookie = stripslashes($cookie);
$cookieSavedArray = json_decode($cookie,true);

print_r($cookieSavedArray);

include 'UserActions.php';
$msg="";
$checkEmail = new UserActions();
$checkEmail->databaseConnection('localhost', 'root', '', 'placement2018');
}

HTML表单

<form action="success.php" enctype="multipart/form-data" method="post">
                    <div class="col-sm-12">
                            <div class="form-group">
                                <label>Password</label>
                                <input type="password" name="pass" placeholder="Enter password" class="form-control" required>
                            </div>
                        <div class="form-group">
                            <label>Confirm Password</label>
                            <input type="password" name="confirmpass" placeholder="Confirm password" class="form-control" required>
                        </div>
                            <input class="btn btn-lg btn-info" type="submit" name="addPassword" value="Submit">
                        </center>

现在,只要我提交表单,URL参数就不再存在了,所以我想存储URL参数,因为我需要根据它来运行查询。

  

如何存储$ _GET参数,以便它们保留在$ _POST请求中?

2 个答案:

答案 0 :(得分:1)

这就是我解决这个问题的方法,我相信这有点像黑客但它有效,我仍然在寻找更好的解决方案。

验证
如果网址中没有参数,则会重定向

 if(!$_REQUEST['email'] && !$_REQUEST['token']) {
     header("Location: register.php");
 }

如果找到参数:

if(isset($_POST['addPassword'])) {
  $email = htmlentities($_POST['email']);
  $token = $_POST['token'];
  $password=$_POST['pass'] ;
  $confirm = $_POST['confirmpass'] ;
  if($password == $confirm) {
      if($checkEmail->checkTokenEmail($email,$token)) {
          $password = password_hash($password,PASSWORD_BCRYPT);
          if($checkEmail->setAccountActiveAndInsertPassword($email,$password)) {
              $msg = "<p style='color:green;text-align:center;'>Successfuly set password, you may login now</p>";
        }
      } else {
        $msg = "<p style='color:red;text-align:center;'>This link is expired</p>";
    }
  }  
}

最后,在$_POST请求之前,我将$email$token存储在隐藏字段中,以便我可以在查询中进一步使用

<form action="success.php" enctype="multipart/form-data" method="post">
<div class="col-sm-12">
    <div class="form-group">
        <label>Password</label>
        <input type="password" name="pass" placeholder="Enter password" class="form-control" required>
    </div>
    <div class="form-group">
        <label>Confirm Password</label>
        <input type="password" name="confirmpass" placeholder="Confirm password" class="form-control" required>
    </div>
    <input type="hidden" value="<?php echo $_REQUEST['email'];?>" name="email">
    <input type="hidden" value="<?php echo $_REQUEST['token'];?>" name="token">
    <center>
        <input class="btn btn-lg btn-info" type="submit" name="addPassword" value="Submit">
    </center>
</div>

答案 1 :(得分:-1)

而不是获取请求使用$_REQUEST['variable_name']它将起作用,您将获得值。

 if(!$_REQUEST['email'] && !$_REQUEST['token'])
 {
     header("Location: register.php");
 }
 else 
 {
  $arrayCookie = array('email' => $_REQUEST['email'] , 'token' =>$_REQUEST['token']);
  $json = json_encode($arrayCookie);
  setcookie('data',$json,time()+(8400));
  $cookie = $_COOKIE['data'];
  $cookie = stripslashes($cookie);
  $cookieSavedArray = json_decode($cookie,true);

  print_r($cookieSavedArray);
  //use require instead of include
  require 'UserActions.php';
  $msg="";
  $checkEmail = new UserActions();
  $checkEmail->databaseConnection('localhost', 'root', '', 'placement2018');
  }