【问题标题】:$_POST is empty in php5.6$_POST 在 php5.6 中为空
【发布时间】:2017-01-21 03:03:39
【问题描述】:

在堆栈溢出中提交表单后,我尝试了几乎所有与空 $_POST 相关的解决方案,但没有一个解决了我的问题。我正在使用 wamp 服务器并使用 phpstorm。

  • 更改了 php.ini 中 post_max_size 和 upload_max_filesize 的值
  • $_GET 工作正常
  • 尝试使用其他 ide 和编辑器
  • isset 返回错误
  • var_dump 显示数组为空
  • 将 method="post" 更改为 method="POST"
    我的代码:
    main.html

    <html>
    <body> 
    <form action="welcome.php" method="post">
    Name: <input type="text" name="name"><br>
    E-mail: <input type="text" name="email"><br>
    <input type="submit">
    </form>
    </body><br>
    </html>
    

    欢迎.php

    <html>
    <body>
    Welcome <?php echo $_POST["name"]; ?><br>
    Your email address is: <?php echo $_POST["email"]; ?>
    </body>
    </html>`
    

错误 -

欢迎

注意:未定义的索引:名称在
C:\Users\user\PhpstormProjects\tryphp\welcome.php 在第 4 行

您的电子邮件地址是:

注意:未定义的索引:电子邮件
C:\Users\user\PhpstormProjects\tryphp\welcome.php 在第 5 行

【问题讨论】:

  • 检查浏览器网络选项卡以查看是否将发布参数发送到服务器
  • 它运行良好。确保两个文件(main.html 和 welcome.php)都在同一个文件夹中。
  • 你在 php.ini 的 variables_order 指令中有 P 字母吗?你试过var_dump($_REQUEST);var_dump(file_get_contents('php://input')); 吗?
  • 不是路径问题,因为加载了welcome.php动作
  • variables_order = "GPCS" var_dump($_REQUEST); 的输出是 array(0) { },var_dump(file_get_contents('php://input')); 的输出是 string(16) "name=df&email=df"。 “df”是我输入的姓名和电子邮件。

标签: php html apache wampserver php-5.6


【解决方案1】:

通过快速测试,您的代码在我的服务器上运行正常。

运行 PHP 配置文件 (/etc/php5/apache2/php.ini) 我发现这些项目可能值得研究

; Whether PHP will read the POST data.
; This option is enabled by default.
; Most likely, you won't want to disable this option globally. It causes $_POST
; and $_FILES to always be empty; the only way you will be able to read the
; POST data will be through the php://input stream wrapper. This can be useful
; to proxy requests or to process the POST data in a memory efficient fashion.
; http://php.net/enable-post-data-reading
;enable_post_data_reading = Off

所以进入你的 php.ini 并搜索 enable_post_data_reading,如果你没有找到它,手动输入它(通常在文件末尾)并确保它是 On

enable_post_data_reading = On

我的版本信息:

PHP 5.5.9-1ubuntu4.17 (cli) (built: May 19 2016 19:05:57)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

如果您仍然一无所获,请查看您的访问日志。它应该显示进入您的服务器的 POST 请求;

255.255.255.255 - - [13/Sep/2016:10:16:00 -0400] "POST /stackoverflow/welcome.php HTTP/1.1" 200 1226 "http://255.255.255.255/stackoverflow/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/255.255.255.255 Safari/537.36"

(忽略IP地址信息,我编辑它以保护我的服务器:))

如果它没有显示 POST 请求,那么这是一个开始的地方。

您也可以在浏览器上按 F12(或 ctrl+shift+i)打开调试控制台,单击网络(在顶部)在您的表单上,单击提交按钮,您将看到一个网络请求出去。单击它,单击“标题”,滚动到底部,然后查看您的浏览器实际发送到服务器的信息。

希望这批东西能对你有所帮助。请告诉我们:)

【讨论】:

  • 我可以在 headers 部分看到正确的表单数据,但是在welcome.php 中 $_POST 仍然是空的
  • 访问日志中没有 POST,只有 GET。是否有可能所有 POST 请求都被转换为 GET?
  • 是的,这是可能的,但不是我有很多经验的东西,只是一个快速的谷歌,这出现了 (stackoverflow.com/questions/26728231/…) 你是否使用代理/URL重写或其他任何东西和welcome.php 可能会篡改您的连接?甚至可能只是将表单上的操作更改为 welcome.php 的完整 URI,以便 mod-rewrite 不会将其更改为 www.site from site。
  • 这也很有趣,尽管可能不适用于这种情况。 stackoverflow.com/questions/11872649/… 不管怎样,这可能不是 PHP 问题,很可能是 Apache 问题。如果您可以通过 SSH 访问您的机器,您可以使用“apache2ctl -l”检查已编译的模块
  • 没有使用任何代理,我已经看到了这两个链接。我会尝试重新安装 wamp。谢谢
【解决方案2】:

如果您在这些字段中提供任何输入,则您的代码需要工作。选中此项,命名提交按钮并在提交该按钮时表单将转到 Welcome.php

Main.html

<html>
<body> 
    <form action="welcome.php" method="POST">
        Name: <input type="text" name="name"><br>
        E-mail: <input type="text" name="email"><br>
        <input type="submit" name="submitForm">
    </form>
</body>
</html>

welcome.php

<html>
<body>
    <?php 
    $name = "";
    $email = "";
    if (isset($_POST["submitForm"])) {
        $name = $_POST["name"];
        $email = $_POST["name"];
    }
    ?>
    Welcome <?php echo $name; ?><br>
    Your email address is: <?php echo $email; ?>
</body>
</html>

如果字段也是空的,那么你的错误就会消失。

如果这不起作用,则表示您的 welcome.php 不在 main.html 的同一文件夹中。检查一下..

【讨论】:

    【解决方案3】:

    在welcome.php 中添加第一行:

    var_dump($_POST);
    

    检查您的帖子是否为空...

    【讨论】:

    • 这是一条评论,并没有为 OP 问题提供任何解决方案!
    • 嗯,解决方案是我们树上的最终叶子。但是为了实现这一点,我们需要知道一些细节。在这种情况下,如果帖子为空,则问题不是来自欢迎文件,应检查 main.html 或服务器配置...
    • var_dump($_REQUEST); 的输出是 array(0) { }
    • var_dump($_SERVER['CONTENT_TYPE']);
    • 2.检查 php.ini 上的 post_max_size 值。如果为0,则不能发送post数据
    猜你喜欢
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2013-04-27
    相关资源
    最近更新 更多