3

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.

William Linton
  • 840
  • 1
  • 7
  • 14
  • 2
    don't you "include" it using HTTP url instead of filesystem path? the path seems all right but it's the most likely reason – Your Common Sense Aug 07 '10 at 07:03
  • I include it using a filesystem path because it's outside of the document root (it has sensitive data and I don't want it to be publicly accessible, whether protected by .htaccess or not). – William Linton Aug 07 '10 at 07:08

1 Answers1

3

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 https://meta.stackexchange.com/questions/12513/should-i-not-answer-my-own-questions )

Community
  • 1
  • 1
William Linton
  • 840
  • 1
  • 7
  • 14