首页 > 解决方案 > PHP Post Empty 根据请求

问题描述

我无法弄清楚为什么这不起作用。我敢肯定这可能是小事,但我一直在挠头。下面是我的 HTML 和 PHP。

表单.html

<form action="result" method="post">
   <input type="text" class="form-control" name="hello">
   <input type="submit" class="btn btn-primary" value="Submit">
</form>

result.php (.htaccess 设置为允许不使用 .PHP 并且在操作中包含 .PHP 时我收到相同的问题)

<?php

  if($_SERVER['REQUEST_METHOD'] == 'POST')
  {
     echo "POST<br>";
  }
                                
  print_r($_POST);

?>

提交表单会将您发送到 result.php 并显示 POST,但$_POST为空。

编辑:.htaccess

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [NE,R=302,L]

提前喝彩。

标签: phphtml

解决方案


在@mickmackusa 和@CBroe 的帮助下,确定问题出在.htaccess文件上。

删除 .PHP 扩展名时,它是重定向而不是重写。解决方案是将其更改.htaccess为以下内容:

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

感谢大家的帮助。


推荐阅读