开发者

Included PHP scripts can't access $_POST when output buffering?

开发者 https://www.devze.com 2023-01-10 09:57 出处:网络
I have an HTML form POSTing to the following index.php: <?php require_once(\"/home/full/path/to/included/file.php\"); ?>

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? Than开发者_Python百科ks.

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.


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 )

0

精彩评论

暂无评论...
验证码 换一张
取 消