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

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路