连接关闭后继续php脚本 [英] Continue php script after connection close

查看:114
本文介绍了连接关闭后继续php脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面/连接关闭后尝试继续PHP脚本。



用户每1小时POLL一次脚本,我想返回一些json输出并希望在后台继续脚本。我使用共享主机,我不能使用cron作业。



这是我试过的。

  ob_start(); 

ignore_user_abort();

echoJSON_OUTPUT GOES HERE;

$ ob_length = ob_get_length();

header(Content-Type:text / plain,TRUE);
header(Content-Length:$ ob_length,TRUE);
header(Connection:Close,TRUE);

flush();
ob_flush();
ob_end_flush();

sleep(3);

echo你看不见我..;

exit();

我使用Codeigniter框架,但它不工作在我的活动服务器。它等待3秒,然后输出。你也看不到我..

$



请帮助我。 >

注意



项目托管在LINUX / WINDOWS / WAMP-SERVER共享主机中。



<$ p $

p> function closeOutput($ stringToOutput){
set_time_limit(0);
ignore_user_abort(true);
header(Connection:close\r\\\
);
header(Content-Encoding:none\r\\\
);
ob_start();
echo $ stringToOutput;
$ size = ob_get_length();
header(Content-Length:$ size,TRUE);
ob_end_flush();
ob_flush();
flush();
}

您可以使用

  $ outputContent ='Contentent Goes Here ...'; 

closeOutput($ outputContent);

sleep(5);

//做一些背景工作...

exit();


I am trying to continue a PHP Script after the page/connection is closed.

Users will POLL the script in every 1 hour, I want to return some json output and want to continue the script in the background. I am using a shared host and I cannot use cron job.

Here is what I've tried.

ob_start();

ignore_user_abort();

echo "JSON_OUTPUT GOES HERE";

$ob_length = ob_get_length();

header("Content-Type : text/plain",TRUE);
header("Content-Length : $ob_length",TRUE);
header("Connection : Close",TRUE);

flush();
ob_flush();
ob_end_flush();

sleep(3);

echo "You cant see me..";

exit();

I am using Codeigniter framework, But its not working on my live server. It waits 3 seconds and then outputting You cant see me.. too.

Please help me.

Note

Project is hosted in LINUX/WINDOWS/WAMP-SERVER shared hosts.

解决方案

After some research i got it work, Sometime it may be useful to some others.

function closeOutput($stringToOutput){   
        set_time_limit(0);
        ignore_user_abort(true);
        header("Connection: close\r\n");
        header("Content-Encoding: none\r\n");  
        ob_start();          
        echo $stringToOutput;   
        $size = ob_get_length();   
        header("Content-Length: $size",TRUE);  
        ob_end_flush();
        ob_flush();
        flush();   
} 

You can use it like

$outputContent = 'Contentent Goes Here...';

closeOutput( $outputContent );

sleep(5);

//do some background works ...

exit();

这篇关于连接关闭后继续php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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