【问题标题】:PHP Post Empty Upon RequestPHP Post Empty 根据请求
【发布时间】:2020-08-20 10:02:51
【问题描述】:

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

form.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]

提前干杯。

【问题讨论】:

  • action="result.php" 而不是 action="result" in &lt;form action="result.php" method="post"&gt;
  • 然后向我们展示您的 .htaccess 实际包含的内容。这听起来像是由于 external 重定向而丢失帖子数据的典型情况,浏览器现在发出 GET 请求。
  • 嗨@CBroe 我已经包含了 .htaccess - 为您的帮助欢​​呼!
  • 嘿@Chilarai,我已经尝试过了,不幸的是,它没有任何区别。我认为 CBroe 可能会在正确的轨道上使用 .htaccess

标签: php html


【解决方案1】:

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

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

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

感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 2021-05-30
    • 2021-03-11
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多