note 123383 added to function.register-shutdown-function

From: Date: Wed, 28 Nov 2018 21:58:53 +0000
Subject: note 123383 added to function.register-shutdown-function
Groups: php.notes 
Request: Send a blank email to php-notes+get-209807@lists.php.net to get a copy of this message
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); } } ---- Server IP: 208.43.231.9 Probable Submitter: 189.32.225.192 ---- Manual Page -- http://php.net/manual/en/function.register-shutdown-function.php Edit -- https://master.php.net/note/edit/123383 Del: integrated -- https://master.php.net/note/delete/123383/integrated Del: useless -- https://master.php.net/note/delete/123383/useless Del: bad code -- https://master.php.net/note/delete/123383/bad+code Del: spam -- https://master.php.net/note/delete/123383/spam Del: non-english -- https://master.php.net/note/delete/123383/non-english Del: in docs -- https://master.php.net/note/delete/123383/in+docs Del: other reasons-- https://master.php.net/note/delete/123383 Reject -- https://master.php.net/note/reject/123383 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#209807) next »