tags:

views:

150

answers:

5

Hi all

I am submitting from a form to another php page which is supposed to process it. The problem is that the $_POST is often empty when I try to read it.

This is the form:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Order Form</title>
</head>


<body>
    <form id="mailorder" name="mailorder" action="mailordertest.php" method="post">
        <input type="hidden" name="A212-M" value="1" /> 
        <input type="hidden" name="A212-XXXL" value="2" /> 
        <input type="hidden" name="A212-XXL" value="3" /> 
        <input type="hidden" name="A212-XL" value="4" /> 

        <input type="submit" value="Confirm Order">
    </form>
</body>
</html>

And this is the processing page:

<?php
    session_start();
    error_reporting(E_ALL);

    print_r($_POST);
?>

This will usually output nothing but Array (), but sometimes the data will come through first time. When the $_POST is empty, I can hit Back and then resubmit and after a few tries, it will work.

I have inspected the whole shebang with FireBug and it seems that the $_POST is getting cleared because the processing page does a 302 redirect back to itself (that obviously uses a GET, and so the POST data is discarded).

I have googled this to death, but none of the answers I have found have been any help. The following are NOT the problem: 1. post_max_size is set to 8M (not MB) 2. There is no CONTENT_TYPE issue

Other PHP apps are working fine on the server (Wordpress etc), and I am using PHP 5.2.9.

I am totally stumped on this one - so thanks for ANY ideas and help!

Edit: I should also mention that I have tried various methods of disabling caching with no success either.

A: 

Check out this link

Long story short:

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.

Woot4Moo
According to the Original Post, there is not a Content-Type Issue.
Chacha102
Thanks, I checked this out, but I dont think thats my problem. I tried making the changes suggested anyway, but no luck with it.
Marc
@Chacha102: Still no need to downvote. This is good, constructive advice for people coming across the question in the future.
Pekka
A: 

The only thing that comes to mind that could produce 302s is a .htaccess rewrite statement somewhere along the line. Can you check whether there are any?

Also, are you using any kind of login on your site?

Pekka
To the first - there is no .htaccess file.And to the second - yes, there is some simple authentication and most of the files in the app redirect to the login page if a user isn't logged in. The code I included come from files in the same directory - but that is exactly how they appear, no login checking or anything else.
Marc
A: 

Not a PHP expert but one thing that comes to mind is the use of session_start.

I will assume you're using cookies for your session (If you're using querystring in some way then I'm sure you'd have caught on by now) - cookies are sent in the header and must be sent before any page content. If you rendered content before calling this perhaps the page is redirecting because it needs an opportunity to add the session cookie. This would also concur with your "repeat attempts work" scenario. Often the culprit is a few white-spaces after a ?> tag.

You could test this by either removing the session call or using a header inspector to see if the redirect is indeed setting a cookie. If this is the case you'll need to call session_start earlier so it can write to the headers. I believe PHP has a call that defers content delivery so that you can add headers during the page cycle, but I don't know the exact call and recommend against it for performance reasons anyway.

Tim Schneider
Please excuse me if I sound like an idiot, but I am rather new to PHP (I am a desktop developer by trade). A cookie is set in the app, and when I watch the POST with FireBug, I see that a cookie is being sent along with the request (containing a username and a sessionid). There is no other cookie work being done in the page with the form.
Marc
Hey, I'm referring to the processing page. If what I'm saying is right you'd expect the "302" response not to have a cookie in it and the final response that has an empty form to contain a new session cookie.
Tim Schneider
A: 

Is this the workflow?

  • Form POSTs to mailordertest.php
  • mailordertest.php redirects to fstest.php
  • fstest.php is writing out the value of $_POST, per the code example.

If this is the workflow then it should be no surprise that the $_POST data is not propagating correctly (as you've noted).

So, check mailordertest.php and search for any header() calls. If you want fstest.php to have access to $_POST data move the session_start call to the top of mailordertest.php and include() fs_test.php at the end of mailordertest.php (or, better yet, include fstest.php at the top and wrap it's functionality in a function to call at the right time.

pygorex1
Thanks for taking the time to answer pygorex1, but the workflow is straight from fstest.php to mailordertest.php with nothing in-between.fstest.php is the first file I included above and mailordertest.php is the second.The code you see above is absolutely everything.
Marc
+1  A: 

I think you may have something going on with your local network and/or computer. If I go to the page at

http://www.ruby.za.net/fstest.php

in Firefox and click submit, I see

Array
(
 [A212-M] => 1
 [A212-XXXL] => 2
 [A212-XXL] => 3
 [A212-XL] => 4
 [submit] => Confirm Order
)
Array
(
 [UNIQUE_ID] => Sx2qR0gOsRsAAFP-3GUAAAAB
 [HTTP_HOST] => www.ruby.za.net
 [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5
 [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
 [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5
 [HTTP_ACCEPT_ENCODING] => gzip,deflate
 [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
 [HTTP_KEEP_ALIVE] => 300
 [HTTP_CONNECTION] => keep-alive
 [HTTP_REFERER] => http://www.ruby.za.net/fstest.php
 [HTTP_COOKIE] => PHPSESSID=1143682669e6297436eea3af99e14d4c
 [HTTP_PRAGMA] => no-cache
 [HTTP_CACHE_CONTROL] => no-cache
 [CONTENT_TYPE] => application/x-www-form-urlencoded
 [CONTENT_LENGTH] => 62
 [PATH] => /usr/local/bin:/usr/bin:/bin
 [SERVER_SIGNATURE] => <address>Apache/2 Server at www.ruby.za.net Port 80</address>

 [SERVER_SOFTWARE] => Apache/2
 [SERVER_NAME] => www.ruby.za.net
 [SERVER_ADDR] => 72.14.177.27
 [SERVER_PORT] => 80
 [REMOTE_ADDR] => 71.193.197.39
 [DOCUMENT_ROOT] => /home/ruby/domains/ruby.za.net/public_html
 [SERVER_ADMIN] => [email protected]
 [SCRIPT_FILENAME] => /home/ruby/domains/ruby.za.net/public_html/mailordertest.php
 [REMOTE_PORT] => 63515
 [GATEWAY_INTERFACE] => CGI/1.1
 [SERVER_PROTOCOL] => HTTP/1.1
 [REQUEST_METHOD] => POST
 [QUERY_STRING] => 
 [REQUEST_URI] => /mailordertest.php
 [SCRIPT_NAME] => /mailordertest.php
 [PHP_SELF] => /mailordertest.php
 [REQUEST_TIME] => 1260235335
 [argv] => Array
  (
  )

 [argc] => 0
)

Whenever I have a problem like this, I revert to using curl to post some data to the page to see what happens

$ curl -d "A212-M=1" -d "A212-XXXL=2" -d "A212-XXL=3" -d "A212-XL=4" -i http://www.ruby.za.net/mailordertest.php
HTTP/1.1 200 OK
Date: Tue, 08 Dec 2009 01:27:13 GMT
Server: Apache/2
X-Powered-By: PHP/5.2.9
Set-Cookie: PHPSESSID=c6baea21344f3f3754064a658ede739c; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Vary: Accept-Encoding,User-Agent
Content-Length: 1236
Content-Type: text/html

Array
(
 [A212-M] => 1
 [A212-XXXL] => 2
 [A212-XXL] => 3
 [A212-XL] => 4
)
Array
(
 [UNIQUE_ID] => Sx2rcUgOsRsAAE-8zJ8AAAAK
 [HTTP_USER_AGENT] => curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
 [HTTP_HOST] => www.ruby.za.net
 [HTTP_ACCEPT] => */*
 [CONTENT_LENGTH] => 41
 [CONTENT_TYPE] => application/x-www-form-urlencoded
 [PATH] => /usr/local/bin:/usr/bin:/bin
 [SERVER_SIGNATURE] => <address>Apache/2 Server at www.ruby.za.net Port 80</address>

 [SERVER_SOFTWARE] => Apache/2
 [SERVER_NAME] => www.ruby.za.net
 [SERVER_ADDR] => 72.14.177.27
 [SERVER_PORT] => 80
 [REMOTE_ADDR] => 71.193.197.39
 [DOCUMENT_ROOT] => /home/ruby/domains/ruby.za.net/public_html
 [SERVER_ADMIN] => [email protected]
 [SCRIPT_FILENAME] => /home/ruby/domains/ruby.za.net/public_html/mailordertest.php
 [REMOTE_PORT] => 63558
 [GATEWAY_INTERFACE] => CGI/1.1
 [SERVER_PROTOCOL] => HTTP/1.1
 [REQUEST_METHOD] => POST
 [QUERY_STRING] => 
 [REQUEST_URI] => /mailordertest.php
 [SCRIPT_NAME] => /mailordertest.php
 [PHP_SELF] => /mailordertest.php
 [REQUEST_TIME] => 1260235633
 [argv] => Array
  (
  )

 [argc] => 0
)

If the curl request results in an empty array, that means there's a problem with your network.

If the curl request works (like the one above), then you have a browser problem. Trying switching over to a clean IE/Safari/Opera and see if you're seeing the same problem. Also, try disabling all your Firefox extensions to see if one is interfering with browser operation.

Alan Storm
Thats an awesome tip!I tried from my side and got a 302... At least this time it told me I was being redirected because [code=BYPASSED_URL] (google time)... I have tried with other browsers including Chrome and IE without success. I ran the curl command once more and got the right output.
Marc
Yeah, that's a network problem on your end, probably something about the way your connection is being proxied.
Alan Storm
Your comment about network problems got me thinking - the ISP I have been using for the last few days has a very aggressive cache server. I swapped over to another account with a different ISP quickly and tried the forms out (test and production) and all worked perfectly. I haven't been able to get another 302 with curl or in the app itself. I have set Pragma: no-cache and Cache-Control: no-cache, must-revalidate but it seems like those aren't helping at all.
Marc
Does the `fstest.php` page do a 302 redirect to itself at any point? If so it should correctly be doing a `303 See Other` instead really. It is still a NetCache bug either way, but 303 is never cacheable whereas 302 can be cacheable but by default isn't, so conceivably it might make a difference. A proxy cache inserting its own 302s with its own bodies is pretty completely broken.
bobince
No, there are no 302 redirects in the code at all. And yes, the proxy is completely broken - good thing the isp used by 70% of the country eh </sarcasm>
Marc
Dear Lord! Name and shame! :-) I wonder what the specific circumstances are that cause it to occur? Clearly it doesn't happen all the time or the entire web would be broken. The only other reference I can find has a curious redirect from `www.example.com/...` to `www.example.com:80/...`, is that a behaviour you're seeing?
bobince
The "ISP" is our national telephone operator Telkom (I'm in South Africa).As far as the redirect from example.com to example.com:80 goes, no, I am not seeing that happening at all.
Marc