Ciao.
Secondo voi questo è un uso
appropriato di compressione dell'ouput:
Gzip class:
Codice PHP:
<?php
class Gzip{
private $fileName= '';
public function __construct($fileName){
if(!file_exists($fileName)){
throw new FileException('File ['.$fileName.'] not found in ['.__CLASS__.']');
}
$this->fileName= $fileName;
}
public function gZipOutput(){
// check if browser supports gzip encoding
if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')){
// start output buffer
ob_start();
// include file into output buffer
include($this->fileName);
// compress data with gzip
$output=gzencode(ob_get_contents(),9);
// clean up output buffer
ob_end_clean();
header('Content-Encoding: gzip');
echo $output;
}
else{
include($this->fileName);
}
}
}
?>
Gz.php
Codice PHP:
<?php
// Per esempio
$obj= new MyClass();
//recupero l'ouput
$output= $obj->display();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php echo $output; ?>
</body>
</html>
?>
index.php
Codice PHP:
<?php
$root = realpath(dirname(__FILE__));
$root = str_replace('\\', '/', $root);
require_once($root."/classes/class.Exception.php");
require_once($root."/classes/class.Gzip.php");
try {
$gzip= new Gzip($root.'/gz.php');
$gzip->gZipOutput();
unset($gzip);
}
catch (FileException $e) {
echo $e;
error_log($e->getMessage()."\n", 3, $root."/log/error.log");
exit();
}
?>
Praticamente in gz.php ho la pagina
index non compressa che includo
per la compressione in index.php
se il browser accetta dati compressi
se no mostro la pagina senza compressione.
Aspetto consigli