registershutdownfunction php,PHP: register_shutdown_function - Manual

I elaborated following functions, so you can close your connection if you can't count on fastcgi_finish_request.

You'll use puts instead of echo, so you can close the connection whenever you want as long as you don't touch echo when you use those functions.

It works it compression and is easy to adapt:

function connsettings($name, $val=null){

static $settings = array();

if(!isset($settings[$name]))

$settings[$name] = false;

if($val !== null)

$settings[$name] = $val;

return $settings[$name];

};//Just saving compression and init var to check if it's true or false

function puts(){

//Using args so it works like echo with comma ands dots

$numargs = func_num_args();

$arg_list = func_get_args();

$string = '';

if($numargs === 0)

return;

else if($numargs > 1)

for($i = 0; $i < $numargs; $i++)

$string .= $arg_list[$i];

else

$string = $arg_list[0];

if(connsettings('init') === false)

{

// buffer all upcoming output - make sure we care about compression:

if(ob_start("ob_gzhandler"))

connsettings('compression', true);

else

ob_start();

connsettings('init', true);

register_shutdown_function('puts', null, true);

}

echo $string;

}

function close(){

if(!headers_sent())//it may work without this verification when no compression but may lead to uncomplete data

{

// send headers to tell the browser to close the connection

ob_end_flush(); // Order here matter, it won't work if it goes after Content-Length

if(connsettings('compression') === false)

header("Content-Encoding: none");

header('Content-Length: ' . ob_get_length());

header('Connection: close');

// flush all output

//ob_end_flush();

ob_flush();

flush();

// close current session

if (session_id()) session_write_close();

set_time_limit(0);

ignore_user_abort(true);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值