PHP使用緩衝產生靜態頁面

來源:互聯網
上載者:User

標籤:

http://www.cnblogs.com/lh460795/archive/2013/04/06/3003105.html

在apache / bin/ab.exe  可以做壓力測試,該工具可以類比多人,並發訪問某個頁面.

 

基本的用法

 ab.exe –n 10000 –c 10

-n 表示請求多少次

-c 表示多少人

如果要測試php自己的緩衝機制, 需要做配置.

php.ini 檔案

display_errors=On

output_buffering=Off

error_reproting= 設定錯誤層級

 

看一段代碼,使用緩衝時,在傳送檔案頭之前可以顯示文字.

<?php

              echo“yyy”;

              header(“content-type:text/htm;charset=utf-8”);

              echo“hello”;

?>

PHP緩衝控制的幾個函數:

 

 1 //PHP緩衝控制的幾個函數: 2 //開啟緩衝 [通過php.ini,也可以在頁面 ob_start()] 3 ob_start(); 4 echo "yyy"; 5 header("content-type:text/htm;charset=utf-8"); 6 echo "hello"; 7 //ob_clean函數可以清空 outputbuffer的內容. 8 //ob_clean(); 9 //ob_end_clean是關閉ob緩衝,同時清空.10 //ob_clean();11 //ob_end_flush() 函數是 把ob緩衝的記憶體輸出,並關閉ob12 //ob_end_flush();13 //ob_end_flush() 函數是 把ob緩衝的記憶體輸出,14 //ob_flush()函數是輸出ob內容,並清空,但不關閉.15 ob_flush();16         17 echo "kkk";//=>ob緩衝.18 19 //header("content-type:text/htm;charset=utf-8");20 21 //ob_get_contents() 可以擷取output_buffering的內容.22 //$contents=ob_get_contents();23 24 //file_put_contents("d:/log.text",$contents);

 

下面來看一個執行個體,用緩衝技術,"假如儲存的快取檔案未超過30秒,則直接取出快取檔案":

 

 1 <?php 2                 $id=$_GET[‘id‘]; 3                 $filename="static_id_".$id.".html"; 4                 $status=filemtime($filename)+30>time();//判斷檔案建立及修改時間距目前時間是否超過30秒 5                 if(file_exists($filename)&&$status){ 6                     $str=file_get_contents($filename); 7                     echo $str; 8                 }else{ 9                     require_once "SqlHelper.class.php";10                     $sqlHelper=new Sqlhelper();11                     $arr=$sqlHelper->execute_dql2("SELECT * FROM news1 WHERE id=$id");12                     if(empty($arr)){13                         echo "資料為空白";14                     }else{15                         /***緩衝開始***/16                         ob_start();//下面的內容將存到緩衝區中,顯示的內容都將存到緩衝區17                         echo $arr[0][‘tile‘];18                         echo "<br/>";19                         echo $arr[0][‘content‘];20                         $content=  ob_get_contents();//從緩衝中擷取內容21                         ob_end_clean();//關閉緩衝並清空22                         /***緩衝結束***/23                         file_put_contents($filename, $content);24                         echo $content;25                     }26                 }27                 28                 29             ?>

PHP使用緩衝產生靜態頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.