views:

91

answers:

1

I have an HTML form POSTing to the following index.php:

<?php require_once("/home/full/path/to/included/file.php"); ?>

And in file.php, I am trying to access $_POST:

ob_start();
session_start();
var_dump($_POST);
$contents = ob_get_contents();
ob_end_clean();
echo $contents;

But var_dump($_POST) returns an empty array.

Moving the ob functions to the index.php has no effect.

If I put the var_dump($_POST) before ob_start(), OR if I remove the output buffering altogether, the problem disappears, but I need output buffering. So in an effort to hunt down the problem, I tried POSTing the form data to a test.php:

ob_start();
$session_start;
var_dump($_POST);
$contents = ob_get_contents();
ob_end_clean();
echo $contents;

And it displayed everything fine. So the problem with my file.php seems to be that it is both included, and using output buffering. Do you see a problem with my setup? Why can't $_POST be accessed by an included, output-buffered script? Do you see a way to fix this, or an alternative? Thanks.

EDIT: One other possible factor: My HTML form tag is this:

<form action="/" method="POST">

I use mod_rewrite to redirect that to index.php. I try action="/index.php" and nothing changes, so it shouldn't matter.

+1  A: 

I realized the cause of my problem. Having removed output buffering, I took a closer look at the error messages and realized I had a header("Location: ...") call in my file.php. I was redirecting to another script in which I was attempting to call the $_POST information. But $_POST information obviously doesn't get transferred along with a header() redirect.

The interesting thing about this is that I had a var_dump($_POST) before the header() call. And I'm guessing the output buffering caused the header() call to be sent first, and therefore no output after that in the buffer was displayed, because a header("Location") redirects without displaying any script output after it. Does that sound right?

(Answering my own question a la http://meta.stackoverflow.com/questions/12513/should-i-not-answer-my-own-questions )

willell
Makes sense. This is why I always put a die() after a var_dump to be sure I get to see the output I want to see :)
Jani Hartikainen
That's a good idea. I'll keep that in mind.
willell