ob_file_callback和fwrite的问题 [英] Problems with ob_file_callback and fwrite

查看:138
本文介绍了ob_file_callback和fwrite的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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,并且我选择了case 1,它声称无法在ob_file_callback上重新声明ob_file_callback,它位于case 2中。

If I choose $forks = 1, it write to case2.txt which is nuts but it does. Not only does it fail to redirect LOCATION, but it won't even show the default page. It claims there's an issue on the header location because headers have already been sent, which is surreal. Then, even though I'm not asking it to redeclare ob_file_callback because it's in case 2 and I've opted for case 1, it claims cannot redeclare ob_file_callback on the ob_file_callback that's sitting inside case 2.

如果我选择$ forks = 2,它不会写入任何文件,只是声称它不能修改标题信息,所以它是另一个标题重定位失败,这也是愚蠢的。但它给我的默认页面,但这只是进一步困惑我。

If I choose $forks = 2, it doesn't write into any file whatsoever and just claims it can't modify header information so it's another header relocation fail, which is also stupid. It does however give me the default page, but that just confuses me further.

我的问题与fwrite,ob_start或ob_end_flush或任何正在写入文件,是它只会写一次。无论是它还是它在每次打开文件时擦除该文件,所产生的问题是,当forks 1确实写入case2.txt时,它始终只有一个字符串坐在那里。

My problem with fwrite, ob_start or ob_end_flush or whatever's doing the writing to file, is that it'll only write it once. Either that or it's erasing the file everytime it opens it, so the resulting issue is that when forks 1 does indeed write to case2.txt, it's always only ever 1 string sitting there.

第一轮更正:

First round of corrections:

    <?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
?>


推荐答案

与1。
You do 发送重定向头,但这只是发送给客户端的头文件。它不会 终止执行!

You have no break statement in your first case branch dealing with the "1". You do send a redirection header, but that is just a header sent to the client. it does not terminate execution!

使用评论中讨论的反馈,我试图让事情变得更清晰:

Using the feedback from the discussion in the comments, I try to make things a little clearer:

<?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

?>

我没有测试过这个代码(所以可能会有一些语法错误...)。我只是想记下一条关于如何使这个工作成功的建议......

I have not tested that code (so there might be some syntax glitch...). I just want to note down a suggestion of how to get this working...

这篇关于ob_file_callback和fwrite的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆