$_POST corrupted by ob_start

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kiwione
Forum Newbie
Posts: 3
Joined: Sat Dec 12, 2009 10:40 pm

$_POST corrupted by ob_start

Post by kiwione »

Hi There,

very new to PHP, so please be gentle with me :?

I have a problem with the $_POST array on a form (to edit user account details) that I've built:

The functioning of the form is:
1) A test is done to see if $_POST['submit'] is set
2) if it is set then the various form controls are validated and any errors result in a $_SESSION element being set with an error message and a header() function called to redisplay the form.
3) if $_POST['submit'] is not set then the db is read for the user ID stored in $_SESSION and the db fields are pre-loaded into other $_SESSION elements
4) The form is displayed - any error message is displayed at the top, and the form controls are displayed with their pre-loaded values from $_SESSION.

All quite straight forward. The problem is though that depending upon what values are entered into the form by the user (or even what field names on the form are used) the $_POST array may or may not be set when the Submit button is clicked.

I discovered eventually that by removing the ob_start() function call at the top of the php, the $_POST array would be set correctly 100% of the time. However this also caused further problems down the script when the validation section tried to issue a header() function call ('headers already sent' type of error).

I've looked and looked at the code, and everything seems ok. I've nearly torn out what little remains of my hair, so any suggestions would be greatly appreciated.

Thanks very much in advance
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: $_POST corrupted by ob_start

Post by requinix »

No offense, but just because you can't see anything wrong doesn't mean everything's right.

1. You shouldn't need to use output buffering to call header() and other similar functions. My opinion, of course, but there are good reasons for it. Consider restructuring your code.
2. Output buffering affects output. $_POST comes from input. The two are not related and should not affect each other. I'd rather see some code than fire off a few random things to check.
bowtakwah
Forum Newbie
Posts: 4
Joined: Sat Dec 12, 2009 2:48 pm

Re: $_POST corrupted by ob_start

Post by bowtakwah »

Code: Select all

<form method="post" action="response.php">
<input type="text" name="hello" />
<input type="submit" name="submit" />
</form>
response.php

Code: Select all

<?php
    ob_start();
    print_r($_POST);
?>
Array ( [hello] => test [submit] => Submit )
kiwione
Forum Newbie
Posts: 3
Joined: Sat Dec 12, 2009 10:40 pm

Re: $_POST corrupted by ob_start

Post by kiwione »

Thanks for the replies.

I'm well aware that just because it looks right doesn't mean it's not wrong, and I was certainly not implying such. What I meant was that I couldn't see anything wrong myself - but as stated I'm very new to php

Aware also that ob_start shouldn't make any difference. The fact is however that (in this case) it does. With ob_start I get the problem and without it I don't.

The original code was from a commercial php based system, and I'm attempting to modify it for my own purposes, so I'm not sure why it was included, unless it has to be able to get the header() function calls to work correctly.

I'd be happy to include code, but there's quite a lot of it and I don't want to break any of the forum rules.

Thanks
kiwione
Forum Newbie
Posts: 3
Joined: Sat Dec 12, 2009 10:40 pm

Re: $_POST corrupted by ob_start

Post by kiwione »

If you would like to see this issue in action, then the test site is:
hhatest.giveit2me.net

If you login using
ID: richard@mytotalsuccess.com
PWD: 333111

and then navigate to "Author Tools/Edit Account" you'll see the form. It will be preloaded with the original set values. The form has diagnostic dumps in it to state whether or not the 'Submit' was triggered and a dump of the contents of $_POST using the print_r function.

If you try one of the following:
a) Leave either the first name of last name blank
b) key anything into the "YouTube Enbed Code" textbox

you will see that the Submit was not triggered and the $_POST array is empty. Modifying the name fields (and not leaving them blank) or modifying any of the other fields will show that the Submit was triggered, and the $_POST array has the correct values.

If I use the ob_flush() function directly before the print_r of the $_POST array, or I leave out ob_start altogether then it will always work correctly (but as already stated, the header() call further down the validation code will fail).

Thanks
alun
Forum Newbie
Posts: 1
Joined: Thu Feb 13, 2014 2:01 pm

Re: $_POST corrupted by ob_start

Post by alun »

I realise this is way too late for the original poster, but I was having the same problem, and came across this topic while trying to find an answer.

I've since got the the bottom of the problem I was having - the fix might have helped the original poster, and might help the next person to wander in here looking for a fix.

What was happening in my case showed the same symptoms: $_POST array empty after calling ob_start, but fine if ob_start removed.

What was going on was that at some point into the script, I was calling header() to output a Location: header to redirect the user to another page. I'd unintentionally set it to redirect to the same page.

Something like:

ob_start();
print_r($_POST);
echo "some output...";
if ( some condition in $_POST )
{
header("Location: <<this-script>>"
}
....

With the ob_start() in place, the code runs through to the header() call then discards all output built up so far, and redirects the user back to this script.
However, this second run through the script doesn't get the POST variables, as there's no way to pass them through the header redirect, so the print_r()
shows an empty $_POST array.

If you take out the ob_start() then the contents of $_POST are output immediately (not buffered) so you see them, but the header() call doesn't do anything
(because there's already been some output).

My fix was to correct the redirect target, but yours will vary depending an what you're trying to do.
Post Reply