繁体   English   中英

如何使用服务器请求方法在同一页面上使用get和post

[英]How to use a get and post on same page using server request method

我试图使用服务器请求方法在同一页面上使用get和post ...我有另一种工作方法,但我想尝试一种新方法。 如何保留$ _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;
}
?>  

解决此问题的一种方法是使用相同的通用代码,无论请求是作为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;}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM