当我 pcntl_fork() 时,如何阻止 php 返回标头?

发布于 2024-10-31 18:20:31 字数 1414 浏览 0 评论 0原文

看来我的 php 为 pcntl_fork() 生成的每个子项打印了 X-Powered-By 和 Content-Type 标头。如果它不在输出中间打印它们,这不会成为问题。

因此,例如,这个玩具脚本:

function very_long_process($shm){
    sleep(20);
    shm_put_var($shm,0,'terminated');
}
function iterate_until_terminated($shm){
    $signal = shm_get_var($shm,0);
    if($signal=='running'){
        $j = shm_get_var($shm,1);
        $j++;
        shm_put_var($shm,1,$j);
        sleep(2);
        iterate_until_terminated($shm);
    }
    else{
        exit;
    }
}
$shm = shm_attach(ftok(tempnam('/tmp','PHP'),'a'),1000000);
shm_put_var($shm,0,'running');
$i=0;
shm_put_var($shm,1,$i);
$pid = pcntl_fork();
if($pid==0){
    iterate_until_terminated($shm);
}
very_long_process($shm);
while (pcntl_waitpid(0, $status) != -1) {
    $status = pcntl_wexitstatus($status);
}
$iterated = shm_get_var($shm,1);
$signal = shm_get_var($shm,0);
echo "<p>iterated $iterated times.";
echo "<p>process was $signal";

生成输出(在浏览器中):

X-Powered-By: PHP/5.2.17
Content-type: text/html

<p>iterated 10 times.<p>process was terminated

我已经读过,并且愿意相信,解决方案是使用 ob_start() 和 ob_end_clean(),但我已经尝试过有几个地方它不起作用,尽管(奇怪的是)使用 ob_end_flush() 我能够将额外标头对的数量保持为两个。那么呃..在哪里缓冲输出?

当然,我也会对不涉及输出缓冲的解决方案感到满意。

感谢您的帮助!

编辑: 这个特定的玩具叉旨在模拟将请求传递给模型,使用视图读取其输出数据流,然后通过模板引擎将其转换为周期性的 ajax 请求。

我知道这可能有很多问题,但我不想在这里偏离主题。有没有办法抑制标题?

It seems that my php prints X-Powered-By and Content-Type headers for every child made by pcntl_fork(). this wouldn't be a problem if it didn't print them in the middle of the output.

So, for instance, this toy script:

function very_long_process($shm){
    sleep(20);
    shm_put_var($shm,0,'terminated');
}
function iterate_until_terminated($shm){
    $signal = shm_get_var($shm,0);
    if($signal=='running'){
        $j = shm_get_var($shm,1);
        $j++;
        shm_put_var($shm,1,$j);
        sleep(2);
        iterate_until_terminated($shm);
    }
    else{
        exit;
    }
}
$shm = shm_attach(ftok(tempnam('/tmp','PHP'),'a'),1000000);
shm_put_var($shm,0,'running');
$i=0;
shm_put_var($shm,1,$i);
$pid = pcntl_fork();
if($pid==0){
    iterate_until_terminated($shm);
}
very_long_process($shm);
while (pcntl_waitpid(0, $status) != -1) {
    $status = pcntl_wexitstatus($status);
}
$iterated = shm_get_var($shm,1);
$signal = shm_get_var($shm,0);
echo "<p>iterated $iterated times.";
echo "<p>process was $signal";

Produces the output (in a browser):

X-Powered-By: PHP/5.2.17
Content-type: text/html

<p>iterated 10 times.<p>process was terminated

I've read, and would like to believe, that the solution is to use ob_start() and ob_end_clean(), but I've tried that in a couple places and it didn't work, although (weirdly) with ob_end_flush() I was able to keep the number of extra header pairs to two. So uh.. where to buffer output?

Of course, I'd be just as happy with a solution that didn't involve output buffering.

Thanks for your help!

Edit:
This particular toy fork is meant to simulate passing a request to a model, reading its output stream of data with a view, and then translating that via a templating engine to be picked up by a periodic ajax request.

I know there are probably lots of things wrong with that, but I don't want to get off-topic here. Is there a way to suppress the headers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除会话 2024-11-07 18:20:31

我很确定 apache 正在添加这些标头,而不是 php。这就是输出缓冲不起作用的原因。你必须在 apache 中关闭这些功能。

I'm pretty sure apache is adding those headers, not php. That is why output buffering is not working. You'll have to get those turned off in apache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文