1.流程
- 动态语言流程
1 2 3 4
| graph LR A[syntax]-->B[complie] B-->C[run] C-->D[show]
|
- 动态程序执行顺序
1 2 3 4
| graph LR A[ConnectDB]-->B[Get Date] B-->C[file Template] C-->D[Show]
|
静态页面展示
2. Buffer
buffer其实是php的缓冲区,一个内存地址空间,主要用于存储数据区域.
php的展示流程
1 2 3 4
| graph LR A[content]-->B[php buffer] B-->C[tcp] C-->D[screen]
|
开启的2种方式
1 2 3 4 5
| # php.ini output buffering=on
# page.php ob_start();
|
3. 页面静态化实现
3.1 实现方式
1 2
| file_put_contents('./index.html', './index.php');
|
函数 |
备注 |
ob_start |
打开输出控制缓冲 |
ob_get_contents |
返回输出缓冲区内容 |
ob_clean |
清空(擦掉)输出缓冲区 |
ob_get_clean |
得到当前缓冲区的内容并删除当前输出缓冲区 |
3.2 静态化页面 Demo
- 设计动态页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
$arr = [ [ 1,'title1','content1'], [ 2,'title1','content1'], [ 3,'title1','content1'], [ 4,'title1','content1'], [ 5,'title1','content1'], ];
foreach($arr as $t){ echo $t[0]; echo $t[1]; echo $t[2]; }
|
- 生成静态
1 2 3 4 5 6 7 8 9 10 11
| ob_start();
require_once('./template.php');
if( file_put_contents('template./html', './.php') ){ echo 'success'; }else{ echo 'false'; }
|
4. 静态化触发方式
原理
1 2 3 4 5
| graph TD
A[用户请求页面]-->B{判断 } B --> |存在且未过期|E[直接访问静态页面] B --> |过期/不存在| D[动态页面并生成新的静态页面]
|
实现
1 2 3 4 5 6 7 8 9 10 11 12
|
if( is_file('index.html') && ( $time()-$filemtime('./index.html'))<300 ){ require('index.html'); }else{ ob_start(); require('template.php'); file_put_contents( 'index.html',ob_get_contents() ); }
|
1 2 3 4 5 6
|
crontab -e
*/5 * * * * * php /pathto/index.php
|
5. 伪静态 及 Rewrite配置
原本URL: http://state.com/newsList.php?type=2&category_id=1
目标网址 http://state.com/newsList.php/2/1.html
分析可得只要修改以下部分并拼接即可
1
| 2=> type=2 1=>category_id=1
|
5.1 伪静态
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| $res = preg_match('/\/(\d+)\/(\d+)\.html/',$_SERVER['PATH_INFO'],$arr); echo '<pre>'; var_dump($arr); echo '</pre>';
if( $res ){ $type = $arr[1]; $category_id = $arr[2];
}else{ } print_r($arr);
|
注意: Nginx的网址参数是 $_SERVER[‘REQUEST_URI’]
6. 伪静态配置
6.1 Apache的配置
- httpd.conf文件中开启相关模式
- LoadModule rewrite module modules/mod rewrite.so
- Include conf/extra/httpd-vhosts.conf
- vhosts文件增加相关域名
1 2 3 4 5 6 7 8 9 10 11
| # vhosts.conf
RewriteEngine on
# 如果有文件存在,则不重写 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}!-d RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}!-f
# 配置伪静态 RewriteRule ^/detail/([0-9]*).html$/detail.php?id=$1
|
6.2 Nginx的伪静态配置
1 2 3 4 5 6 7 8
| # vhost.conf
location ~\.php{ if (!-e $request_filename){ rewrite ^/detail/([0-9]*).html$ /detail.php?id=$1 last; break; } }
|
reload nginx