dqjo4340 2018-02-28 06:09
浏览 80
已采纳

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

I am trying to store $_GET in variables and re-use them in a POST request, but the problem is that as soon as a POST request is sent the URL becomes empty and there is nothing to store in variables or the data stored in variables is removed as there is nothing in the URL

success.php?email=16564017@gmail.com&token=M3XK5HeCZy Current URL

after a POST request is sent the URL is success.php leaving nothing to store,

I'm trying to update passwords of users on basis of this method

I've tried this with $_SESSION, but cannot figure it out, My current work

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

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

Now, as soon as I submit the form, the URL parameters are not there anymore, so I want to store the URL parameters as I need to run queries based on it.

How do I store $_GET parameters such that they remain in a $_POST request as well?

  • 写回答

2条回答 默认 最新

  • dongweihuan8832 2018-03-02 07:26
    关注

    This is how I've resolved this problem, I believe this is sort of a hack but it works, I am still looking for a better solution.

    Validations
    If there are no parameters in the URL it'll redirect

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

    If parameters found then :

    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>";
        }
      }  
    }
    

    and finally, before the $_POST request I am storing $email and $token in a hidden field so that I can further use it in queries

    <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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行