PHP $_POST Array Empty
Fellow PHP programmers, I encountered a most peculiar problem today that I must share. I had a situation where someone was sending me POST data but the PHP $_POST array was empty. I setup a debugging script to see the GET and POST arrays and sure enough everything was empty. I asked my friend Google and found a neat way to get the raw incoming message using PHP:
That convinced me that they were in fact sending me what looked like POST data but for some reason the PHP $_POST array was still empty. Long story short, what I discovered and hopefully what will save someone out there some time, is that if the Content-Type is empty or not recognized in the HTTP message then the PHP $_POST array is empty. Not sure if this is a bug or is by design...
Anyway if the guy on the other end can't figure out how to send a properly formatted HTTP message, you can force the Content-Type as a last resort using something like this above where you want to access the $_POST array:
$type = "application/x-www-form-urlencoded";
$_SERVER['CONTENT_TYPE'] = $type;
}
You’re currently reading “ PHP $_POST Array Empty ,” an entry on BRADINO
- Published:
- 11.9.07 / 6pm
- Category:
- PHP























Thanks a lot!
Setting the content-type manually didn’t do the trick for me, but I never knew $_POST data could be accessed using the file_get_contents trick.
That alone helped a lot in my case, thanks!
Very interesting! I’m going to run some tests with apache to see if Apache is dropping the POST on purpose due to the invalid content-type header, or whether php does it..
I had exactly this problem. I used the code you posted (file_get_contents) because my post array was empty. I decided to have a look at my html from code again. I spent an hour and a half trying to figure this out. I got so frustrated that I rewrote my whole form. I realised that the reason my post array was empty was because I was specifying an enctype and I didn’t need to. I removed the enctype attribute and printed my post array using print_r($_POST) and hey presto, there were my posted values.
I hope this will be of help other people in the future.
works perfectly
Thanks Brad, and thanks Ashley – the tag-team of advice solved a problem I’ve been struggling with :)
the problem in 90% of this cases is that ['var'] you look for in post doesnt exists. why? coz for some reason form doesnt organise post data by input’s ID but by NAME attribute… simply add name attribute next to id’s and all works well …
Thank you. I removed the enctype and it worked!
Just want to add that I had the same prolem, for me the issue was the following:
I didnt start off with <?php, but with HTML code and therefor I think the $_POST values got lost cause after I put the following on top of the HTML code everything was solved:
my problem was solved
hope this helps some of you guys out there
note: none of the above worked for me.
code is the following (it got lost it seems)
(adding things that should not be there to not let it dissapear)
echo “<?php
$_POST value capture
|$var = $_POST['var']“;
echo ” should not be there
This saved the day!! For several days now I and several other people over at experts-exchange have been trying to figure this out…http://www.experts-exchange.com/Web_Development/WebApplications/Q_24097457.html#a23517475
Thanks to all the people for sharing this information. I’ve been about 4 or 5 hours until I figured out my problem was different.
I have two domains for the same web. When you enter the site with the secondary domain (B), rewrite redirects you to the main one (A).
The problem was that the base tag was pointed to B when you’re accesing the web using A.
So the solution for me was changing inside the head tag:
to:
Hope this help someone!!!
Sorry the code disappear. It’s just changing the base href value from A to B. Anyway, I’ll try putting some quotes:
“”
to:
“”
My $_POST array was empty to. I putted a dynamic form in another form element so the names existed twice at that moment. I gave it his own form element and problem was solved.
Is there any way to post data from an aspx page to another aspx page…..
Thanks Vedran,
I pulled my hair trying to figure out why?
But WHY name and not ID??
Vedran, thanks a lot, you saved my live… name fields… name fields… name fields… not id. It worked for me. THANKS!!!!
I had a simular problem on my server at dreamhost.com. if I used the web site address “http://mardox.com” instead of the “http://www.mardox.com” The $_POST was empty. I found that the problem is in the Re-Write of the url… when someone types in mardox.com the server rewrites the url to http://www.mardox.com.
I found that by making sure that people go to http://www.mardox.com the problem is fixed.
I hope this helps
Hey guys,
I had the same problem where the $_POST would not be populated.
For me, it turned out that ‘post_max_size’ in my php.ini file was not a valid value.
I would suggest changing it to:
post_max_size = 8M
Make sure it’s not ‘8MB’ or a really large number (which mine was and PHP didn’t like).
Hey guys,
I had the same problem where the $_POST would not be populated.
For me, it turned out that ‘post_max_size’ in my php.ini file was not a valid value.
I would suggest changing it to:
post_max_size = 8M
Make sure it’s not ‘8MB’ or a really large number (which mine was and PHP didn’t like!).