<?php
$forks = 2;
switch ($forks) {
case 1:
$ob_file = fopen('case1.txt','w');
function ob_file_callback($buffer)
{
global $ob_file;
fwrite($ob_file,$buffer);
}
ob_start('ob_file_callback');
echo $ip = $_SERVER['REMOTE_ADDR'];
ob_end_flush();
header("LOCATION: case1.php");
case 2:
$ob_file = fopen('case2.txt','w');
function ob_file_callback($buffer)
{
global $ob_file;
fwrite($ob_file,$buffer);
}
ob_start('ob_file_callback');
echo $ip = $_SERVER['REMOTE_ADDR'];
ob_end_flush();
;
header("LOCATION: case2.php");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
如果我选择$ forks = 1,它会写入case2.txt,这是坚果但确实如此。它不仅无法重定向LOCATION,而且甚至不会显示默认页面。它声称标题位置存在问题,因为标题已经发送,这是超现实的。然后,即使我没有要求它重新声明ob_file_callback,因为它是在案例2中并且我选择了案例1,它声称不能在位于案例2中的ob_file_callback上重新声明ob_file_callback。
如果我选择$ forks = 2,它不会写入任何文件,只是声称它不能修改头信息,所以它是另一个头重定位失败,这也是愚蠢的。但它确实给了我默认页面,但这让我更加困惑。
我对fwrite,ob_start或ob_end_flush的问题或者其他正在写文件的问题是,它只会写一次。无论是那个还是它每次打开它时都会擦除文件,因此产生的问题是,当forks 1确实写入case2.txt时,它总是只有1个字符串坐在那里。
第一轮更正:
<?php
function ob_file_callback($buffer)
{
global $ob_file;
fwrite($ob_file,$buffer);
}
$forks = 1;
switch ($forks) {
case 1:
$ob_file = fopen('case1.txt','w');
ob_start('ob_file_callback');
echo $ip = $_SERVER['REMOTE_ADDR'];
ob_end_flush();
header("LOCATION: case1.php");
exit;
break;
case 2:
$ob_file = fopen('case2.txt','w');
ob_start('ob_file_callback');
echo $ip = $_SERVER['REMOTE_ADDR'];
ob_end_flush();
header("LOCATION: case2.php");
}
exit
?>
答案 0 :(得分:0)
你的第一个案例分支中没有处理“1”的中断语句。 您执行发送重定向标头,但这只是发送到客户端的标头。它不终止执行!
使用评论中讨论的反馈,我试着让事情变得更加清晰:
<?php
// writes a given string $buffer into a file handle spcified by the global $file
function ob_file_callback($buffer)
{
global $ob_file;
fwrite($ob_file,$buffer);
return $buffer;
}
// main():
// #####
$forks = 1; // testing only !!
//$forks = 2; // testing only !!
// #####
global $ob_file = FALSE;
switch ($forks) {
case 1:
$ob_file = fopen('case1.txt','w');
ob_start('ob_file_callback');
echo sprintf('ip = %s',$_SERVER['REMOTE_ADDR']);
ob_end_flush();
fclose($ob_file);
header("Location: case1.php");
break;
case 2:
$ob_file = fopen('case2.txt','w');
ob_start('ob_file_callback');
echo sprintf('ip = %s',$_SERVER['REMOTE_ADDR']);
ob_end_flush();
fclose($ob_file);
header("Location: case2.php");
} // switch
?>
我没有测试过那段代码(所以可能会有一些语法故障......)。我只是想记下如何让这个工作起作用的建议......