如何使用服务器请求方法在同一页面上使用获取和发布

时间:2019-05-17 18:19:04

标签: php

我正在尝试使用服务器请求方法在同一页面上使用获取和发布...我有另一种工作方法,但是我想尝试一种新方法。 如何保留$ _GET ['email_address']变量,以便可以在POST方法中使用它。

<?php $timestamp = strftime("%Y-%m-%d %H:%M:%S", time()); ?>
<?php
$email_address = $str = "";
$email_address_error = $str_error = "";

if($_SERVER["REQUEST_METHOD"] == "GET"){

if(empty($_GET["email_address"])){
    $email_address_error = "<div class=''>email address is empty, please check your link.</div>";
}else{
    $email_address = test_input($_GET["email_address"]);
}

if(empty($_GET["str"])){
    $str_error = "<div class=''>email address is empty, please check your link.</div>";
}else{
    $str = test_input($_GET["str"]);
}   
}

我希望post方法可以获取$ _GET变量,以便将其传递给下一个...这就是我所需要的。

$password = $confirm_password = "";
$password_error = $confirm_password_error = "";
$email_address = test_input($_GET["email_address"]);

if($_SERVER["REQUEST_METHOD"] == "POST"){

if(empty($_POST["password"])){
    $password_error = "<div class=''>Password is required</div>";
}else{
    $password = test_input($_POST["password"]);
    //check if password is atleast 7 characters
    if(!preg_match("/^(?=.*?[a-z]).{7,}$/",$password)){
        $password_error = "<div class=''>Password must be atleast 7 characters</div>";
    }
}   

if(empty($_POST["confirm_password"])){
    $confirm_password_error  = "<div class=''>Alternate password is required</div>";
}else{
    $confirm_password = test_input($_POST["confirm_password"]);

    if(!preg_match("/^(?=.*?[a-z]).{7,}$/",$confirm_password)){
        $confirm_password_error = "<div class=''>Password must be atleast 7 characters</div>";
    }else{
        if($_POST['confirm_password'] != $password){
            $confirm_password_error = "<div class=''>Password does not match!!!</div>";
        }
    }
}

if($password_error == "" && $confirm_password_error == ""){
    $sql  = "UPDATE customer_registration SET password='$password', str='', updated_at='$timestamp' WHERE email_address='$email_address'";
        $database->query($sql);
        $mail = new Mail();
        $mail->email_address  =  $email_address;
        $mail->reset_password();    
        $session->message('<div class="btn bg-success">Congratulations!!! Your password has been updated. </div>');
    redirect_to('login.php');
}   

if(empty($_POST["message"])){
    $message = "";
}   else{
    $message = test_input($_POST["message"]);
}   
}

function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = htmlentities($data);
    return $data;
}
?>  

1 个答案:

答案 0 :(得分:0)

一种解决方法是使用相同的通用代码,而不管请求是以GET还是POST提交。 (因此,您甚至不需要测试$ _SERVER ['Request_Method']。)

这就是$ _REQUEST超全局变量的用途。从文档中:

“默认情况下包含$ _GET,$ _ POST和$ _COOKIE内容的关联数组。” (https://www.php.net/manual/en/reserved.variables.request.php

当$ _GET,$ _ POST和/或$ _COOKIE的元素具有相同的索引时, request_order 指令确定将使用哪个填充$ _REQUEST。 (https://www.php.net/manual/en/ini.core.php#ini.request-order。)

当然,如果您绝对需要在代码中保留$ _GET超全局变量,并且不能更改为$ _REQUEST,那么另一种选择是从$ _POST手动填充$ _GET超全局变量:

foreach($_POST as $index => $value){
  if(!isset($_GET[$index])){$_GET[$index] = $value;}
}