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

问题描述 投票:1回答:2

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

[email protected]&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请求中?

php post get
2个回答
1
投票

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

验证 如果URL中没有参数,它将重定向

 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
投票

而不是获取请求使用$_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');
  }
© www.soinside.com 2019 - 2024. All rights reserved.