views:

4937

answers:

5

Hi folks,

I'm baffled on this after much googling. This issue is simple, I have a custom CMS i've built that works perfectly on my dev box (Ubuntu/PHP5+/MySQL5+). I just moved it up to the production box for my client and now all form submissions are showing up as empty $_POST arrays. I found a trick to verify the data is actually being passed using "file_get_contents('php://input');" and the data is showing up fine there -- the $_POST/$_REQUEST arrays are always empty. I've also verified the content-type headers are correct as well via firebug (application/x-www-form-urlencoded; charset=utf-8). This issue is happening regardless of whether a form is submitting via AJAX or a regular form submit.

Any help is greatly appreciated!

+1  A: 

Are you sure you understand server and client side languages? The fact that you are saying that whether or not a form is submitted via AJAX means you may not. How are you using $_POST and AJAX?

Try creating a new so-test.php file with the following content and putting it on your server:

<?php

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

?>

<form method="post" action="so-test.php">
    <input type="hidden" name="test1" value="one" />
    <input type="hidden" name="test2" value="two" />
    <input type="hidden" name="test3" value="three" />
    <input type="submit" value="Test Me" />
</form>

Once that file is up, post a URL for further help.

Andrew G. Johnson
last <pre> should be </pre>
Daniel
Good call Daniel, made the edit
Andrew G. Johnson
jQuery, $.post and $.get... You can specify the method type of an ajax request. With all due respect, I know quite well what im talking about and i apologize if my wording was confusing/misleading.
With all due respect, you don't. Show a code sample and I will prove this quickly.
Andrew G. Johnson
Hey Andrew G Johnson, can you try to be more condescending? It's not coming through clear enough.
eyelidlessness
Andrew G. Johnson: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
MrMage
Andrew, I appreciate your time on this but do you feel it necessary to be rude? You presume that I am an idiot for some reason, yet my question is perfectly valid and your comment about $_POST and ajax implies that YOU aren't all that well versed on AJAX --http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktypeAnyways, this post is not about flaming people out. I sincerely appreciate everyone's input and time on this and I just got word from the hosting company that they reconfigured the box I'm on and possibly some .ini settings may have indeed changed.
Are you trying to use $_POST variables on the page making the AJAX call or the one receiving it? If it is the one making it my original comment is correct. If it is the one receiving then it doesn't matter that it was made via AJAX and my sample code would help iron out the issue. I like that you've decided to ignore my advice and just say "GOD I'M NOT AN IDIOT JUST HELP ME" -- good luck with getting help when you're not willing to follow up.
Andrew G. Johnson
Andrew, I modified your code to demonstrate the problem. $_POST array is populated as expected when a form has less than 10 fields. That means your initial test script worked fine, thanks for that that. It's odd that once you exceed 9 fields in the form the $_POST array is empty, does this sound like a Suhosin config issue?http://minghsiung.com/admin/so-test.php
See, as suspected all POST data isn't being rejected as is implied in your question. Is it just a maximum of 10 fields or does it have anything to do with the length of all field names/values?
Andrew G. Johnson
Mike D: it very probably is `suhosin.post.max_vars`. You should have your provider increase that number.
MrMage
+3  A: 

Make sure that, in php.ini:

  • track_vars (it's only available on very old PHP versions) is set to On
  • variables_order contains the letter P
  • post_max_size is set to a reasonable value (e.g. 8 MB)
  • (if using suhosin patch) suhosin.post.max_vars is large enough.

I suppose the second suggestion of mine will solve your problem.

MrMage
Thanks MrMage, appreciate your insight and will check those ini settings out and let you know if that did the trick. Thanks!
+1  A: 

Don't have an elegant solution at this point but wanted to share my findings for the future reference of others who encounter this problem. The source of the problem was 2 overriden php values in an .htaccess file. I had simply added these 2 values to increase the filesize limit for file uploads from the default 8MB to something larger -- I observed that simply having these 2 values in the htaccess file at all, whether larger or smaller than the default, caused the issue.

php_value post_max_size xxMB
php_value upload_max_filesize xxMB

I added additional variables to hopefully raise the limits for all the suhosin.post.xxx/suhosin.upload.xxx vars but these didn't have any effect with this problem unfortunately.

In summary, I can't really explain the "why" here, but have identified the root cause. My feeling is that this is ultimately a suhosin/htaccess issue, but unfortunately one that I wasn't able to resolve other than to remove the 2 php overridden values above.

Hope this helps someone in the future as I killed a handful of hours figuring this out. Thanks to all who took the time to help me with this (MrMage, Andrew)

It might be worth asking this question over on Serverfault, particularly if the issue lies in the server set-up/htaccess.
David Thomas
If i am not mistaken, the size should be specified like 'xxM', and not 'xxMB', so i wonder if that might have something to do with it...
jcinacio
A: 

Here's another possible cause -- my form was submitting to domain.com without the WWW. and I had set up an automatic redirect to add the "WWW." The $_POST array was getting emptied in the process. So to fix it all I had to do was submit to www.domain.com

icesar
A: 

REFERENCE: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

POST method

We are going to make some modifications so POST method will be used when sending the request...

var url = "get_data.php"; var params = "lorem=ipsum&name=binny"; http.open("POST", url, true);

//Send the proper header information along with the request http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { alert(http.responseText); } } http.send(params);

Some http headers must be set along with any POST request. So we set them in these lines...

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", params.length); http.setRequestHeader("Connection", "close");

With the above lines we are basically saying that the data send is in the format of a form submission. We also give the length of the parameters we are sending.

http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { alert(http.responseText); } }

We set a handler for the 'ready state' change event. This is the same handler we used for the GET method. You can use the http.responseText here - insert into a div using innerHTML(AHAH), eval it(JSON) or anything else.

http.send(params);

Finally, we send the parameters with the request. The given url is loaded only after this line is called. In the GET method, the parameter will be a null value. But in the POST method, the data to be send will be send as the argument of the send function. The params variable was declared in the second line as "lorem=ipsum&name=binny" - so we send two parameters - 'lorem' and 'name' with the values 'ipsum' and 'binny' respectively.

Chandu