【问题标题】:$_POST in php 5.3.5 does not workphp 5.3.5 中的 $_POST 不起作用
【发布时间】:2011-07-05 11:42:38
【问题描述】:

我正在使用 PHP 5.3.5 处理页面,$_POST 似乎不包含从我的表单提交的数据。

这是 HTML 文件:

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html>

这是 PHP 文件:

<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html> 

问题是表单值没有传递,所以我得到这样的输出:

欢迎 ! 你几岁了。

而不是这个:

欢迎约翰! 你今年 28 岁。

我做错了什么?

【问题讨论】:

  • 你确定你已经发布到它了吗?你能看到标题中包含信息的 POST 正文吗?
  • 如果您在welcome.php 上查看源代码,您看到的是php 还是只是空白?
  • 也许是一个小东西,比如你的welcome.php 文件的拼写。

标签: php


【解决方案1】:

试试&lt;?php var_dump($_POST); ?&gt; 看看里面有什么。没有办法 $_POST 坏了——它会是别的东西。可能是服务器设置禁用了 $_POST。

【讨论】:

    【解决方案2】:

    您的 HTML 表单 方法 是否适用于 POST

    <form method="post" action="process.php">
    <fieldset><legend>your data</legend>
    <input type="text" name="txtname" id="id_name" />
    <input type="text" name="txtage" id="id_age" />
    </fieldset>
    <button type="submit">OK</button>
    </form>
    

    【讨论】:

      【解决方案3】:

      你还没有发布,或者有什么东西正在破坏$_POST

      如果您在尝试访问之前没有在代码中提及$_POST,那么您还没有发布到您的脚本中。

      【讨论】:

        【解决方案4】:

        它在这里工作,但在 PHP 5.3.1 中。试试这个:

        <!doctype html>
        <html>
        <head>
        <title>form</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        </head>
        <body>
        
        <form method="post" action="process.php">
        <fieldset><legend>your data</legend>
        <input type="text" name="txtname" id="id_name" />
        <input type="text" name="txtage" id="id_age" />
        </fieldset>
        <button type="submit">OK</button>
        </form>
        
        </body>
        </html>
        
        
        <?php
        if(isset($_POST["txtname"]))
            $txtname = $_POST["txtname"];
        else
            unset($txtname);
        if(isset($_POST["txtage"]))
            $txtage = $_POST["txtage"];
        else
            unset($txtname);
        ?>
        <!doctype html>
        <html>
        <head>
        <title>form</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        </head>
        <body>
        
        <p>Welcome <?=$txtname; ?>!<br />
        You are <?=$txtage; ?> years old.</p>
        </body>
        </html>
        

        【讨论】:

          【解决方案5】:

          这可能是因为你打开了magic_quotes_gpc。你应该把它关掉: http://www.php.net/manual/en/security.magicquotes.disabling.php

          【讨论】:

            【解决方案6】:

            尝试在 POST 中使用大写:

            <form action="file.php" method="POST">
            

            代替:

            <form action="file.php" method="post">
            

            【讨论】:

              【解决方案7】:

              PHP 5.3 不再使用全局、预设变量,如 $_POST,以避免漏洞。

              据我了解,问题在于从未使用过 $_POST 或 $_GET 的程序员可能会将其视为任何其他变量,从而使自己面临安全威胁。

              我还没有找到检索 $_POST 数据的“正确”方法,但我使用的方法似乎相当卫生。

              <?php parse_url(file_get_contents("php://input"), $_POST); 
              

              这会将已解析的 HTTP POST 字符串传输到 $_POST 变量

              【讨论】:

                【解决方案8】:

                你检查过你的 php.ini 吗?
                一旦我将post_max_size 设置为与upload_max_filesize 相同,我就破坏了我的发布方法。

                我认为post_max_size 必须小于upload_max_filesize
                在 RHEL 6.0 中使用 PHP 5.3.3 测试

                【讨论】:

                  【解决方案9】:

                  我也遇到了同样的问题。我使用 Windows 记事本编辑 .php 文件,不小心将文件保存为 Unicode 编码,这就是问题所在。然后我将它保存为 UTF-8 编码,它就像一个魅力。希望这可能会有所帮助。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-04-27
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2023-03-16
                    相关资源
                    最近更新 更多