$_POST 是 PHP 中的空数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12414994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:31:39  来源:igfitidea点击:

$_POST is empty data in PHP

phpformscodeigniterpost

提问by fredphp

I'm having an odd issue with my $_POST data not coming through. First, this works fine on my local host, but doesn't work on my shared host on the web. Here's the form:

我的 $_POST 数据没有通过,我遇到了一个奇怪的问题。首先,这在我的本地主机上运行良好,但在网络上的共享主机上不起作用。这是表格:

<form action="/add" method="POST" enctype="multipart/form-data">

<div>
    Name : <input type='text' name='form[name]' class='form-text ' value="" style='width: 350px' placeholder="I.E. Boulder Vote Bus" maxlength="30"/>
</div>
<div>
    Description : <textarea name='form[description]' class='form-bigbox '/></textarea>
</div>
<div class='clearfix pull-left'>
    Logo: <input type="file" name='form[image_name]' class='' value="" accept="image/jpeg, image/jpg, image/png"/>
</div>
<div class='clearfix'></div>
<div>
    <span id='date' class='change'>
        Date : <input type='text' name='form[date]' id='datepicker' class='form-text ' placeholder="mm/dd/yyyy" value="" style='width: 160px;'/> 
    </span>
    Time : <input type='text' name='form[hr]' class='form-text ' style='width: 40px;' placeholder="01" value=""/> : <input type='text' style='width: 40px;' name='form[min]' class='form-text ' placeholder="00" value=""/> <select name='form[ampm]' class='form-text form-select' style='width: 60px;'><option value='am' >AM</option><option value='pm' >PM</option></select>
</div>
<div>
    Your Email : <input type='text' name='form[email]' class='form-text '  value="" style='width: 350px' placeholder="[email protected]"/>
</div>
<div>
    Zip code : <input type='text' name='form[zipcode]' class='form-text '  value="" placeholder="80304"/> Radius : <input type='text' name='form[radius]' class='form-text '  value="" placeholder="25" style='width:50px;'/> mi
</div>
                    <div>
                            <input type='submit' class='form-submit btn-warning' value='Create a Ride' />
</div>
</form>

Problem is, after I submit the form, I have no $_POST data. If I do echo serialize($_POST);I get a:0:{}. However, if I do, echo file_get_contents('php://input');I get the following:

问题是,提交表单后,我没有 $_POST 数据。如果我这样做,echo serialize($_POST);我会得到a:0:{}. 但是,如果我这样做,echo file_get_contents('php://input');我会得到以下信息:

------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[name]"



Some Name

------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[description]"



Some Description

------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[image_name]"; filename=""

Content-Type: application/octet-stream





------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[date]"





------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[hr]"





------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[min]"





------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[ampm]"



am

------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[email]"





------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[zipcode]"





------WebKitFormBoundaryLCabCxfMMHCvWsAO

Content-Disposition: form-data; name="form[radius]"





------WebKitFormBoundaryLCabCxfMMHCvWsAO--

Furthermore, and odder, if I take the form down to just 3 elements, it works. Any 3 elements are fine, but 4 and it doesn't work. Additionally, other forms on the site (with more than 3 elements) work just fine. It's just this one.

此外,更奇怪的是,如果我将表单缩减为 3 个元素,它就可以工作。任何 3 个元素都可以,但是 4 个元素不起作用。此外,网站上的其他表单(包含 3 个以上的元素)也能正常工作。就这一个。

Here are some relevant PHP settings from my shared host:

以下是我的共享主机中的一些相关 PHP 设置:

PHP Version 5.3.16
max_execution_time  300 
max_file_uploads    20  
max_input_time  -1  -1
max_input_vars  1000    1000
memory_limit    128M    128M
post_max_size   500M    500M 

If it's a server setting, any idea what it could be? Seems like a size issue, but there just isn't that much data being passed (all of these are small values).

如果是服务器设置,您知道它可能是什么吗?似乎是大小问题,但传递的数据并不多(所有这些都是小值)。

I have an htaccess, but I don't think it's related (I'm using codeigniter, and it's the standard CI htaccess, and my other forms submit a-ok):

我有一个 htaccess,但我不认为它相关(我使用的是 codeigniter,它是标准的 CI htaccess,我的其他表单提交了一个-ok):

AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/ [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/ [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/ [L]
</IfModule>

Here's what print_r($_SERVER) produces (slightly redacted for security sake):

这是 print_r($_SERVER) 产生的内容(为了安全起见略有编辑):

Array
(
    [CONTENT_LENGTH] => 1077
    [CONTENT_TYPE] => multipart/form-data; boundary=----WebKitFormBoundarydBnbAkAUNJntI6dz
    [DOCUMENT_ROOT] => [redaced]
    [GATEWAY_INTERFACE] => CGI/1.1
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.3
    [HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_CONNECTION] => keep-alive
    [HTTP_COOKIE] => vrsession=8pO1KwOg4OPzCYqsJHX0Qr6FPfLteTimRjIKQPRC%2F83GlFRTepX%2FnrfE1yDxYxDXiPa2JcX4kQviV6lezxrXmJHjqVbWMe%2FgRne%2Bp9P4%2FfxlZrxta2Zl4DACs2C2nsp1SkqYj6k6xhP6Wc9Zlpxgsma752%2BGxHkvwnT6%2F%2FTey699wnV5je99kZe%2FIhSa04DZvoGXoFc6bx1%2B%2FZRtSvojD86ktpWK91AUFUvltcoj8%2F31jeX0h%2BR7VEOPMZLBeqJrcn7qZGeQ6N2XvCIsh6gqTRZIPdsvyAIW%2FTTibBiY0qUvh50yE5MH060D8utqsiv4S54UrXypBsCP3H2w66pLXHTQKKrK%2FAsn1SEWb2pew%2BEOE4prm%2BJKGxcAq0efCGhkpsHZoP9ZDKDekMzaJTIxUin99RUeXVZTF0yqsMD5A%2FFF0AdZuz343VWsJcV9zru4hPHaAky5NMoZa6R1be9PwA%3D%3D; __utma=74945813.1164249566.1347560100.1347560100.1347560100.1; __utmb=74945813.176.10.1347560100; __utmc=74945813; __utmz=74945813.1347560100.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
    [HTTP_HOST] => [redacted]
    [HTTP_ORIGIN] => [redacted]
    [HTTP_REFERER] => [redacted]/add
    [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
    [PATH] => /bin:/usr/bin
    [PHPRC] => [redacted]
    [QUERY_STRING] => 
    [REDIRECT_QUERY_STRING] => /add
    [REDIRECT_STATUS] => 200
    [REDIRECT_UNIQUE_ID] => UFJZCUoyAC0AAE5v7ps
    [REDIRECT_URL] => /add
    [REMOTE_ADDR] => [redacted]
    [REMOTE_PORT] => 48098
    [REQUEST_METHOD] => POST
    [REQUEST_URI] => /add
    [SCRIPT_FILENAME] => [redacted]/index.php
    [SCRIPT_NAME] => /index.php
    [SERVER_ADDR] => [redacted]
    [SERVER_ADMIN] => [redacted]
    [SERVER_NAME] => [redacted]
    [SERVER_PORT] => 80
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_SIGNATURE] => <ADDRESS>Apache/1.3.42 Server at [redacted] Port 80</ADDRESS>

    [SERVER_SOFTWARE] => Apache/1.3.42 (Unix) Sun-ONE-ASP/4.0.2 Resin/2.1.13 mod_fastcgi/2.4.6 mod_log_bytes/1.2 mod_bwlimited/1.4 mod_auth_passthrough/1.8 FrontPage/5.0.2.2635 mod_ssl/2.8.31 OpenSSL/0.9.7a
    [UNIQUE_ID] => UFJZCUoyAC0AAE5v7ps
    [PHP_SELF] => /index.php
    [REQUEST_TIME] => 1347574025
    [argv] => Array
        (
            [0] => /add
        )

    [argc] => 1
)

回答by Shaun Morgan

  1. I ran this form on my server and it worked with no problems.
  2. I copied the form onto test.php
  3. Sent formdata to test2.php
  4. Displayed output with var_dump($_POST)
  5. Changed: action="/add"(which looks like a directory to me by the way)

    To: action="test2.php"

  6. Which Produced the following output:

    array(1) { ["form"]=> array(9) { ["name"]=> string(0) "" ["description"]=> string(0) "" ["date"]=> string(0) "" ["hr"]=> string(0) "" ["min"]=> string(0) "" ["ampm"]=> string(2) "am" ["email"]=> string(0) "" ["zipcode"]=> string(0) "" ["radius"]=> string(0) "" } }

  1. 我在我的服务器上运行了这个表单,它没有问题。
  2. 我把表格复制到 test.php
  3. 将表单数据发送到 test2.php
  4. 显示输出 var_dump($_POST)
  5. 更改:(action="/add"顺便说一下,它看起来像一个目录)

    到: action="test2.php"

  6. 其中产生了以下输出:

    array(1) { ["form"]=> array(9) { ["name"]=> string(0) "" ["description"]=> string(0) "" ["date"]=> string(0) "" ["hr"]=> string(0) "" ["min"]=> string(0) "" ["ampm"]=> string(2) "am" ["email"]=> string(0) "" ["zipcode"]=> string(0) "" ["radius"]=> string(0) "" } }

Try sending output to a different script i.e "test2.php"

尝试将输出发送到不同的脚本,即 "test2.php"

回答by Kasper Agg

I read you commented: "No change. I'm using codeigniter, so I have the standard 301 in an htaccess." somewhere. If you use 301, you are being redirected PERMANENTLY (and sometimes "forever"). Check your browsers response from server (try to preserver log) and see if your post is being redirected. If so, your POST data will be lost.

我读到你评论说:“没有变化。我使用的是 codeigniter,所以我在 htaccess 中有标准的 301。” 某处。如果您使用 301,您将被永久重定向(有时是“永远”)。检查来自服务器的浏览器响应(尝试保存日志)并查看您的帖子是否被重定向。如果是这样,您的 POST 数据将丢失。

How to preserve POST data via ajax request after a .htaccess redirect?

如何在 .htaccess 重定向后通过 ajax 请求保留 POST 数据?

回答by Clover

<form action="/add"

Change the '/add' part with the target page name: example.php

使用目标页面名称更改“/add”部分:example.php

回答by Phantom

Remember: when you read from forms the inputs' name is case sensitive! Example:

请记住:当您从表单中读取时,输入的名称区分大小写!例子:

<form>
  <input name="hello" />
</form>

Let's say it contains the value bob. In the php file I wrote:

假设它包含值bob。在我写的php文件中:

<?
   $var = $_POST['Hello'];
?>

It will read nothing, because for the php interpreter hellois not Hello- those are two different values/field names.

它什么也不会读,因为对于 php 解释器来说,hello不是Hello——这是两个不同的值/字段名称。

回答by gandaliter

I think PHP might have a problem with file inputs and non-file inputs using the same POST subarray. I.e. having a file upload called 'form[image_name]' and other fields called 'form[something_else]' doesn't work. Try with a different name for the file upload field and see if it works.

我认为 PHP 可能在使用相同的 POST 子数组的文件输入和非文件输入方面存在问题。即有一个名为“form[image_name]”的文件上传和名为“form[something_else]”的其他字段不起作用。尝试为文件上传字段使用不同的名称,看看它是否有效。

PHP: Common Pitfalls - File Uploads

PHP:常见陷阱 - 文件上传