我在test.php里有以下测试代码:
<?php
$step = $_GET['step'];
switch($step) {
case 1:
include 'foo.php'; # line 5
file_put_contents('foo.php', '<?php print "bar\\n"; ?>');
header('Location: test.php?step=2');
break;
case 2:
print "step 2:\n";
include 'foo.php';
break;
}
?>
foo.php最初的内容如下:
<?php print "foo\n"; ?>
当我在浏览器中调用test.php?step=1时,我期望看到以下输出:
step 2:
bar
但是我得到了这个输出:
step 2:
foo
当我注释掉第5行的include时,我得到了想要的结果。结论是,PHP缓存了foo.php的内容。当我使用step=2重新加载页面时,也得到了想要的结果。
现在...为什么会这样,并且如何避免这种情况?
?step=2
,因为你在发送Location
头之前打印了数据(这应该会导致错误)。 - cOle2