您的位置:首页 > 编程语言 > PHP开发

php实现页面静态化

2017-09-08 16:12 387 查看

PHP文件执行顺序

语法解析 => 编译 => 运行

动态程序

1,连接数据库服务器或者缓存服务器。

2,获取数据

3,填充到模版

4,呈现给用户

配置项

output_buffering : 开启缓存

函数

ob_start() : 打开输出缓存

ob_get_contents() : 返回输出缓存区的内容

ob_get_clean():得到当前缓存区的内容并删除当前输出缓存。 ob_clean():清空输出缓存区。

file_put_contents():将一个字符串写入文件。

filemtime():取得文件修改时间

生成静态页面

页面添加缓存时间

if(is_file(‘./index.html’) && time()-filetime(‘./index.html’) < 300){

require_once(‘./index.html’);

}else{

require_once(‘./db.php’);

conn=mysqlconnect(‘localhost′,′root′,′aa′);sql = “select * from tableA”;

result=mysqlquery(sql,conn);news = array();

while(row=mysqlfetcharray(result)){

news[]=row;

}

ob_start();

//引入模版文件

require_once(‘./template/smarty.php’);

file_put_contents(‘index.html’,ob_get_contents());

}

开始处理伪静态文件的请求

if(preg_match(‘/^\/(\d+)\/(\d+).html/’),$_SERVER[‘PATH_INFO’],$arr){

$type = $arr[1];

$cate_id = $arr[2];

}else{

//TODO

}

nginx下reqrite配置

server{

listen 80;

server_name sina.com;

index index.php index.html;

root /root/static;

location /{

if(!-e $request_filename){

rewrite ^/detail/([0-9]*).html\$/ /detail.php?id=\$1 last;

break;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: