Skip to content

Instantly share code, notes, and snippets.

@peterujah
Created January 27, 2020 11:51
Show Gist options
  • Select an option

  • Save peterujah/257bbeda0ecb82144fc9d6f2caaddfd0 to your computer and use it in GitHub Desktop.

Select an option

Save peterujah/257bbeda0ecb82144fc9d6f2caaddfd0 to your computer and use it in GitHub Desktop.
Encode data/php output buffer and close brower connection
<?php
class Encode{
private $isEncode;
public function __construct($serverEncode="none") {
$this->isEncode = $serverEncode;
}
public function compress( $data, $type ) {
$supportsGzip = strpos($this->isEncode, 'gzip' ) !== false;
$data = ($type == "JSON" ? json_encode($data, true) : $data);
if ( $supportsGzip ) {
$content = gzencode( trim( preg_replace( '/\s+/', ' ', $data ) ), 9);
header('Content-Encoding: gzip');
} else {
$content = $data;
header("Content-Encoding: none\r\n");
}
$offset = 60 * 60 * 30;
$expire = "expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
if($type == "JSON"){
header("content-type: application/json; charset: UTF-8");
}else{
header("content-type: text/plain; charset: UTF-8");
}
header("cache-control: must-rSomething is wrongidate");
header( $expire );
header( 'Content-Length: ' . strlen( $content ) );
header('Vary: Accept-Encoding');
return $content;
}
/**
* Close the connection to the browser but continue processing the operation
* @param $body
*/
public function closeConnection($body, $responseCode, $responseType = "HTML"){
// Set to zero so no time limit is imposed from here on out.
set_time_limit(0);
// Client disconnect should NOT abort our script execution
ignore_user_abort(true);
// Clean the output buffer and turn off output buffering
ob_end_clean();
// Turn on output buffering again,
ob_start();
//Compress data and show it
echo $this->compress($body, $responseType);
// Return the length of the output buffer
// Ignore this because we compress and get new length.
//$size = ob_get_length();
// send headers to tell the browser to close the connection
// remember, the headers must be called prior to any actual
// input being sent via our flush(es) below.
header("Connection: close\r\n");
//header("Content-Encoding: none\r\n");
//header("Content-Length: $size");
// Set the HTTP response code
// this is only available in PHP 5.4.0 or greater
if(!empty($responseCode)){
http_response_code($responseCode);
}
// Flush (send) the output buffer and turn off output buffering
ob_end_flush();
// Flush (send) the output buffer
@ob_flush();
// Flush system output buffer
flush();
}
public static function removeWhitespace($buffer){
$conf = array(
"find" => array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/<!--(.*)-->/Uis', // Remove HTML comments
'/[[:blank:]]+/'
),
"replace" => array(
'>',
'<',
'\1',
'',
' '
)
);
return preg_replace($conf["find"], $conf["replace"], str_replace(array("\n","\r","\t"),'',$buffer));
}
}
<?php
include_once("Encode.class.php");
$encode = new Encode($_SERVER['HTTP_ACCEPT_ENCODING']);
ob_start('Encode::removeWhitespace');
?>
<html>
<bod>
<div>
<h3>Hello world</h3>
<p>This will be encoded</p>
</div>
</bod>
</html>
<?php $encode->closeConnection(ob_get_contents(), 200, 'HTML');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment