第一篇、页面静态化

页面静态化概念

我们大多数情况下是直接访问php文件,php脚本在服务器端执行并

返回信息,对于一些大型的网站,访问量很大,频繁的动态操作和操作

数据库会加重服务器的负担.在实际开发中,通常使用缓存技术()或者页面静态化来解决

页面静态化分为:

① 真静态 :把 php->html 访问时就不操作数据库

② 伪静态:只是把网址静态 (访问数据库)

Apache/bin/ab压力测试工具

介绍压力测试工具是为了显示动态页面和静态页面在大访问量的情况下的相应时间,如apache/bin/ab.exe 程序可以来做效率测试

基本使用:

ab [options] [http[s]://]hostname[:port]/path

options是选项:

-n  执行访问次数

-c  用户并发数量

Apache2.2之后的版本有ab压力测试工具可以直接使用

/usr/local/apache2/bin/ab –c 10 –n 1000 http://ip/index.php

表示10个用户发送1000次请求

我们可以在htdocs中建立内容相同的index.htmlindex.php页面

测试:/usr/local/apache2/bin/ab –c 10 –n 1000 http://192.168.211.128/index.html

 

/usr/local/apache2/bin/ab –c 10 –n 1000 http://192.168.211.128/index.php

可以看到html的速度要明显快于php页面。

使用php缓存机制完成页面静态化

我们可以使用php自带的缓存机制来完成页面静态化,但在这里我要

说明一点,仅靠php自身的缓存机制并不能完美的解决页面静态化,

往往需要和其它静态化技术(通常是伪静态技术)结合使用,

例子:当访问一个页面时,先判断是否存在缓存,如果存在,则直接输出缓存文件中的内容。否则,则先查询数据库,获得数据,然后生成缓存文件。

搭建wamp环境

我们使用appserver搭建:

搭建过程安装apache,安装mysql,安装php,它会集成phpMyAdmin等软件然后可以直接使用了,它的网站目录在appserverwww目录。

为了是环境支持php缓存,要修改c:\windows\php.ini

Display_errors=on

Output_buffering=on  //可以会影响执行正常网站程序效率

使用php缓存机制完成页面静态化

例如我们做一个新闻模块的页面,如果页面对实时性要求不高,并且比较稳定,当第一个用户访问新闻页面后,我们可以使用ob缓存机制,把内容缓存到html页面,下次使用直接调用即可。

我们建立一个新闻页面后还要建立测试数据库。

Create table news(id int primary key auto_increment,title varchar(128)) not null ,content varchar(256) not null,filename varchar(32));

Insert into news(title,content)  values("hello","早上好");

News_list.php   //新闻首页

<?php

 //查询新闻列表

// mysql_query('set name utf8')

 $conn=mysql_connect("localhost","root","5991461");

   if(!$conn){

     die("连接失败");

        }

        mysql_select_db("test",$conn);

        $sql="select * from news";

        $res=mysql_query($sql);

        

 header("content-type:text/html;charset=utf-8");

 echo "<h1>news list:";

 echo "<a href='#'>add news</a><hr/>";

 echo "<table>";

 echo "<tr><td>id</td><td>title</td><td>content</td></tr>";

 while ($row=mysql_fetch_assoc($res)){

 echo "<tr><td>{$row['id']}</td><td>{$row['title']}</td><td><a href='show_news.php?id={$row['id']}'>detail</a></td></tr>";

 }

 echo "</table>";

 //关闭连接

 mysql_free_result($res);

 mysql_close($conn);

 ?>

Show_News.php  //显示新闻页

<?php

//接收id

 //$id=@$_GET['id'];

 $conn=mysql_connect("localhost","root","5991461");

   if(!$conn){

     die("连接失败");

        }

        mysql_select_db("test",$conn);

        $sql="select * from news where id=$id";

        $res=mysql_query($sql);

        

        //开启缓存

        ob_start();

        if ($row=mysql_fetch_assoc($res)){

 

header("content-type:text/html;charset=utf-8");

echo "<table border='1px' bordercolor='green' cellspacing='0'

width=400px heigh=300px >";

echo "<tr><td>detail</td></tr>";

echo "<tr><td>{$row['title']}</td></tr>";

echo "<tr><td>{$row['content']}</td></tr>";

echo "</table>";

}

else{

 echo "not found!";

}

//构建一个文件名

$html_content=ob_get_contents();

//ob-$html_filename(必要时要考虑路径)

$html_filename="news_id".$id.".html";

file_put_contents($html_filename,$html_content);

 

 //关闭连接

 mysql_free_result($res);

 mysql_close($conn);

 

?>

因为开启了ob缓存,我们会看到生成了news_id2.htm的静态页,我们使用静态页来访问会发现内容和动态页一样的。

 

if(file_exists($html_filename)){

 //存在就直接访问

 echo "static pager";

 echo file_get_contents($html_filename);

 exit;

 }   //加入开头,如果静态页面存在,就直接跳转到静态页,否则才调用数据库。

但是ob缓存方式有个缺点,如果数据库更新了页面不会自动更新:解决办法,我们可以设置一个时间来判断静态页面是否过期,如过期则重新创建。

<?php

//接收id

 //$id=@$_GET['id'];

 $html_filename="news_id".$id.".html";

 if(file_exists($html_filename)&&filemtime($html_filename)+30>time()){

 //存在就直接访问

 echo "static pager";

 echo file_get_contents($html_filename);

 exit;

 ?>

例如我们做一个数据库更新:

Update news set content=good evening where id=1

然后比较使用了时间戳前后页面的变化。

使用前:

加入代码后文件就会自动更新了

Ob缓存的改进:

1/实时性不够好,30秒延时。

2/页面仍然带php的痕迹,show_news.php?id={$row['id']}

当我们添加新的内容的时候,就同步生成一个静态文件。

Add_news.html  //添加新闻的页面

<html>

<head>

<title>新闻标题</title>

</head>

<!--我们提交一个添加页面时,便会调用模板生成一个静态页面-->

<form action="newsAction.php" method="post">

<table>

<tr><td>新闻标题</td><td><input type="test" name="title"/></td></tr>

<tr><td>新闻内容</td><td><textarea cols="50" rows="10" name="content">

</textarea></td></tr>

<tr><td><input type="submit" value="添加"/></td><td><input

type="reset" value="重新填写"/></td></tr>

</table>

</form>

</html>

 然后给add news 赋予超链接即可完成跳转:

echo "<a href='add_news.html'>add news</a><hr/>";

newsAction.php    //建立一个自动相应页

<?php

//页面完成添加,放入数据库的同时建立html页面

$oper=$_POST['oper'];

 

if($oper=="add"){

 

//接收titlecontent

  $title=$_POST['title'];

  $content=$_POST['content'];

 

 $conn=mysql_connect("localhost","root","5991461");

   if(!$conn){

     die("连接失败");

        }

        mysql_select_db("test",$conn);

        $sql="insert into news(title,content) values('$title','$content')";

        

       if(mysql_query($sql,$conn)){

       //获取刚刚插入的数据id

       $id=mysql_insert_id();

       //建立文件名

       $html_filename="news_id".$id.".html";

       echo "filename=".$html_filename;

       }

      

        //关闭连接

 mysql_close($conn);

       }

?>

然后我们要做的是生成静态页面。定义一个函数replace,然后传递,断开连接即可。

<?php

//页面完成添加,放入数据库的同时建立html页面

//替换,$title代替%title%

header("content-type:text/html;charset=gbk");

       function replace($row,$title,$content){

       $row=str_replace("%title%",$title,$row);

       $row=str_replace("%content%",$content,$row);

       return $row;

       }

$oper=$_REQUEST['oper'];

 

if($oper=="add"){

 

//接收titlecontent

  $title=$_POST['title'];

  $content=$_POST['content'];

 

 $conn=mysql_connect("localhost","root","5991461");

   if(!$conn){

     die("连接失败");

        }

        mysql_select_db("test",$conn);

        $sql="insert into news(title,content) values('$title','$content')";

        

       if(mysql_query($sql,$conn)){

       //获取刚刚插入的数据id

       $id=mysql_insert_id();

       //建立文件名

       $html_filename="news_id".$id.".html";

       //echo "filename=".$html_filename;

       $fp_tmp=fopen("template.tpl","r");

       $fp_html_file=fopen($html_filename,"w+");

       //读取template文件转化为html文件

       while(!feof($fp_tmp)) { //如果没到结尾

       //读取一行

       $row=fgets($fp_tmp);

       //替换

       $new_row=replace($row,$title,$content);

       "fwrite($fp_hmtl_file,$new_row)";

       }

       //关闭文件

       fclose($fp_tmp);

       fclose($fp_html_file);

      

       echo "insert success<a href='news_list.php'>return</a>";

       }

      

        //关闭连接

 mysql_close($conn);

       }

?>

这时我们修改,news_list.php,添加静态页面连接。

 echo "<tr><td>{$row['id']}</td><td>{$row['title']}</td><td><a href='news_id{$row['id']}.html'>detail</a></td></tr>";

 

<?php

//接收id

 //$id=@$_GET['id'];

 $html_filename="news_id".$id.".html";

 echo file_get_contents($html_filename);

?>

页面静态化(真静态)的优点和缺点 


html
静态页(真静态)的好处有三点;一是减少服务器对数据响应的负荷,二是加载不用调动数据库,响应速度快。三是便于优化引擎。
缺点也有几点:一是空间占用比较大。二是生成的文件多了,服务器对html文件的响应负担也较重。