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;
}
?>
解决方案
解决此问题的一种方法是使用相同的通用代码,无论请求是作为 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;}
}
推荐阅读
- python - python joblib返回`TypeError:无法使用相同的代码腌制'weakref'对象,但输入数据不同
- java - 错误:java.lang.ClassCastException:java.lang.String 无法转换为 com.example.animeguide.AnimeListOne
- c++ - 在 catkin_make / Cmake 中链接 MKL
- elasticsearch - 对所需的弹性搜索查询规则发出警报
- javascript - 如何在不加载新组件的情况下使用 Vue Router 进行滚动导航?
- android - Kotlin 中如何使用 Picasso 将 URL 转换为位图?
- sql-server - 在复杂查询中查找相关的 SELECT 子句
- flutter - 将焦点自动从 TextFormField 移动到其他(电话验证)
- yocto - yoctoproject 中自己的层中的“Nothing PROVIDES”
- javascript - 在 HTML 文件中的 JS 脚本中导入模块