PHP頁面靜態化:真靜態兩種方案

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   檔案   資料   

----------------------------------------------------------------------------------------------

方案1:如果靜態檔案存在,且產生時間30秒內,直接返回靜態頁面(有時間延遲)
/*
|------------------
| <www.chenwei.ws>
|------------------
*/
header(‘content-type:text/html;charset=utf-8‘);$id = $_GET[‘id‘] ? intval($_GET[‘id‘]) : ‘‘;if($id === ‘‘) die(‘請輸入要查詢的新聞id!‘);$html_file = "news-id-".$id.".html";//1.主要代碼if(file_exists($html_file) && filemtime($html_file) + 30 >= time()){ echo ‘靜態頁面:‘; echo file_get_contents($html_file);exit;}//這裡也可以使用DB工具類$con = mysql_connect(‘localhost‘, ‘root‘, ‘123456‘);if(!$con){ die(‘串連失敗!‘);}mysql_select_db(‘testdb‘, $con);$sql = "select * from bo_question where question_id = $id";$res = mysql_query($sql, $con);if($row = mysql_fetch_assoc($res)){ ob_start();//2.啟動ob緩衝 header(‘content-type:text/html;charset=utf-8‘); echo ‘<head><meta http-equiv="content-type" content="text/html;charset=utf-8" /></head>‘; echo ‘<table style="border:1px solid red;" cellspacing="0" width="400px" height="200px">‘; echo ‘<tr><td>問題詳細內容</td></tr>‘; echo "<tr><td>標題:{$row[‘question_title‘]}</td></tr>"; echo "<tr><td>詳細:{$row[‘question_detail‘]}</td></tr>"; echo ‘</table>‘; $ob_str = ob_get_contents(); //3.把ob_str儲存到一個靜態檔案頁面,取檔案名稱有講究:1.唯一標識該新聞 2.利於seo file_put_contents("news-id-".$id.".html", $ob_str);
  
  //關閉資料庫連接(非必須; 非長串連下,指令碼執行完會自動關閉)
  mysql_close($con);}
else{ echo ‘沒有查詢到資源!‘;}

 @黑眼詩人


 
方案2:使用模板替換技術(沒有時間延遲)
/*
|------------------
| <www.chenwei.ws>
|------------------
*/
$oper = $_POST[‘oper‘];//添加操作if($oper === ‘add‘){    $title = $_POST[‘title‘];    $content = $_POST[‘content‘];        //如果嚴格按MVC,這裡應該調用model了    $con = mysql_connect(‘localhost‘, ‘root‘, ‘123456‘);    if(!$con)    {        die(‘串連失敗!‘);    }    mysql_select_db(‘news‘, $con);    $sql = "insert into question(null, ‘$title‘, ‘$content‘, ‘‘)";    if(mysql_query($sql, $con))    {        //1.產生靜態檔案         $id = mysql_insert_id();        $html_filename = ‘news-id‘.$id.‘.html‘;        $html_fp = fopen($html_filename, ‘w‘);                //2.把模板檔案讀取(news.html)        $fp = fopen(‘news.tpl‘, ‘r‘);        //r 唯讀方式開啟; r+ 讀寫方式開啟; w 寫入方式開啟:檔案內容將被清空!如果檔案不存在將建立; a 以追加的方式開啟                //3.迴圈讀取        //如果沒有讀到檔案的最後,就一直讀取        while(!feof($fp))        {            //一行行讀            $row = fgets($fp);            //把預留位置替換掉 => 可以自訂完整的替換規則函數            $row = str_replace(‘%title%‘, $title, $row);//如果不重新賦值$row, $row值不會改變            $row = str_replace(‘%content%‘, $content, $row);                        fwrite($html_fp, $row);//4.將內容寫入靜態檔案        }
//5.檔案必須關閉 fclose($html_fp); fclose($fp); echo "添加成功。<a href=‘newslist.php‘>點擊查看新聞!</a>"; } else { die(‘添加失敗!‘); }}//此時在新聞列表內,點擊查看詳情的連結,可以改成產生的靜態頁面地址,直接進入靜態檔案。//news.tpl模板檔案/*<html> <head> <meta charset="utf-8" /> <title>%title%</title> </head> <body> <h1>%title%</h1> <pre>%content%</pre> </body></html>*/

---------------------------------------------------------------------------------------------

聯繫我們

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