Page 1 of 1

$_POST array always empty...

Posted: Thu Jan 21, 2010 10:11 am
by stetou
Hi!
The issue is that I use AJAX from a javascript page to retreive some data from a database. the request returns a json string.
I use a POST request since the URL can be very large and 100% of my users are on IE. In firebug I see that the params are sent but $_POST is empty in the PHP script.
If I call
echo file_get_contents('php://input');
I see the params. So why is $_POST empty??

I tried using a GET request and send params that I know will return a short json string. It worked. I tried the same request with POST and it doesn't.

Until yesterday that was working perfectly. I certainly did something to get this behavior but not in the code since I didn't modify it since several weeks.

I did a lot of search and others had the same problem but I can not find a solution yet.
Thanks in advance for any tips so I can continu investigating...

Steve

Re: $_POST array always empty...

Posted: Thu Jan 21, 2010 10:36 am
by requinix
What kind of "params" do you see? key=value format or something else?

If it was working but a couple days ago, what changed?

Re: $_POST array always empty...

Posted: Thu Jan 21, 2010 10:55 am
by stetou
Here are the params..
nbRequete=1&rss=24&inddef='regional_2006_tresdef'&age=&ageProp=-1&densitePop=-1&repMaj=-1&getIlots=0

"If it was working but a couple days ago, what changed?"
Good question...As I said nothing has changed in the code.
The only thing I can see is yesterday I ran out of disk space on the server. I fixed that though.
I'm still searching on what could have changed on this server...

Re: $_POST array always empty...

Posted: Thu Jan 21, 2010 11:04 am
by ShadowIce
use session_start() & $_SESSION['']

Re: $_POST array always empty...

Posted: Thu Jan 21, 2010 11:51 am
by stetou
use session_start() & $_SESSION['']
I only use PHP to query my database I didn't developped much with this language and I never used session_start() & $_SESSION['']

Why do you suggest to you use that please? Any good link to use these properly?
Do I need to create a session variable for all the params of the POST....

Sorry if these are dummies questions.....
thanks for your help,
Steve

Re: $_POST array always empty...

Posted: Thu Jan 21, 2010 11:54 am
by Eran
Using sessions is irrelevant here. Make sure the request you are sending is indeed a post request and not a get. You can check the type of the request using $_SERVER['REQUEST_METHOD']

Re: $_POST array always empty...

Posted: Thu Jan 21, 2010 2:14 pm
by stetou
checked. It is a POST request

Re: $_POST array always empty...

Posted: Thu Jan 21, 2010 2:40 pm
by Eran
Can you show us the actual code?

Re: $_POST array always empty...

Posted: Thu Jan 21, 2010 2:56 pm
by stetou
javascript using extJS

Code: Select all

Ext.Ajax.request({
url: 'requeteVulnerabilite.php',
method:'POST',
params: { nbRequete: nbRequete, rss:thisRSS, inddef: cb_inddefValue, age: cb_AgeValue, ageProp: cb_AgeProp, densitePop: cb_DensitePopValue, repMaj: cb_RepMajProp, getIlots: cb_getIlots},
failure: function()
{
do stuff...},
success: function(result, request )
{do stuff...
}
Post from firebug
nbRequete=1&rss=24&inddef=&age=&ageProp=-1&densitePop=-1&repMaj=-1&getIlots=1

in PHP $_POST is empty

Code: Select all

$rss = $_POST["rss"];
but file_get_contents('php://input');
contains this
nbRequete=1&rss=24&inddef=&age=&ageProp=-1&densitePop=-1&repMaj=-1&getIlots=1

So I have a workaround, I parse file_get_contents('php://input'); and assign all my variables.
$rss = parsePost(file_get_contents('php://input') )

But I'd like to understand why $_post is empty
thanks
steve

Re: $_POST array always empty...

Posted: Thu Jan 21, 2010 3:43 pm
by Eran
Where's the rest of the PHP script? that can't be all of it?
What does var_dump($_POST) and var_dump($_GET) show?
What does firebug say about the type of request made?

Re: $_POST array always empty...

Posted: Fri Jan 22, 2010 7:50 am
by stetou
var_dump($_POST) returns array(0) {}
This is the problem by the way.

Firebug says it is a POST request, $_SERVER['REQUEST_METHOD'] says it is a POST request. The method is set to POST in the code. I can say that it is using a POST.

The rest of PHP script is useless. The very first line read the $_POST array and it is empty. I created a very simple PHP script that returns the $_POST content and it is empty.

AS I said $_POST is empty but this
$data = file_get_contents('php://input');
returns the params string as shown in a previous post.

thanks for your time and help,
steve

Re: $_POST array always empty...

Posted: Fri Jan 22, 2010 8:02 am
by Eran
Check the content type in the firebug request. It should read "application/x-www-form-urlencoded". If not, that is the problem

Re: $_POST array always empty...

Posted: Fri Jan 22, 2010 8:27 am
by stetou
the content type seems to be text/html

Here is what firebug gives as info
Request Headers
Host geotest.ca
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language fr-ca,en-ca;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
Referer http://geotest.ca/CA/
Content-Length 77
Content-Type text/plain; charset=UTF-8
I set 'Content-Type': 'application/x-www-form-urlencoded' in the Ext.Ajax.request but it still send text/plain; charset=UTF-8

Can you provide more details on why that could be the problem please? I might go on extjs forum to ask about the config of ext.ajax
thanks
steve

Re: $_POST array always empty...

Posted: Fri Jan 22, 2010 8:52 am
by Eran
I'm not sure about the internals, but the server is expecting a POST submission to be either 'application/x-www-form-urlencoded' or 'multipart/form-data' (the latter when transferring files as well). Those are translated internally into the $_POST array. As the content type is "text/plain" the data is sent, but is not automatically parsed into the $_POST array.