php ob_start(ob_gzhandler)進行網頁壓縮_PHP教程

來源:互聯網
上載者:User
本文章來總結關於php ob_start(ob_gzhandler)進行網頁壓縮傳輸的實現有需要的朋友可參考一下。

先來看ob_start用法

使用PHP ob_start()函數開啟browser的cache,這樣可以保證cache的內容在你調用flush(),ob_end_flush()(或程式執行完畢)之前不會被輸出

代碼如下 複製代碼

ob_start(); //開啟緩衝區
phpinfo(); //使用phpinfo函數
$info=ob_get_contents(); //得到緩衝區的內容並且賦值給$info
$file=fopen(’info.txt’,'w’); //開啟檔案info.txt
fwrite($file,$info); //寫入資訊到info.txt
fclose($file); //關閉檔案info.txt

?>

PHP ob_start()函數一個很大的特點;也可以使用ob_start的參數,在cache被寫入後,然後自動運行命令,比如ob_start(”ob_gzhandler”);而我們最常用的做法是用ob_get_contents()得到cache中的內容

面的代碼是一個壓縮網頁的例子,我 們利用ob_gzip函數,使用ob_start將輸出內容壓縮後放到“緩衝區”後再輸出。

代碼如下 複製代碼

//啟用壓縮
if(function_exists('ob_gzip'))
{
ob_start('ob_gzip');
}
//準備一些待壓縮的內容
for($i=0; $i<100; $i++)
{
echo('這裡是測試內容
');
}
//輸出壓縮成果
ob_end_flush();


//這是ob_gzip壓縮函數
function ob_gzip ($content)
{
if( !headers_sent() && extension_loaded ("zlib") && strstr ( $_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")){
$content = gzencode($content,9);
header ("Content- Encoding: gzip");
header ("Vary: Accept- Encoding");
header ("Content- Length: ".strlen ($content));
}
return ($content) ;
}

http://www.bkjia.com/PHPjc/633100.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633100.htmlTechArticle本文章來總結關於php ob_start(ob_gzhandler)進行網頁壓縮傳輸的實現有需要的朋友可參考一下。 先來看ob_start用法 使用PHP ob_start()函數開啟brows...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.