- 静态页面相对于动态页面更容易被搜索引擎收录
- 访问静态页面不需要经过程序处理,因此可以提高运行速度
- 减轻服务器负担
纯静态
全部纯净态
- ob_start() 打开缓冲区
- ob_get_contents() 获取缓冲区内容
- ob_end_flush() 输出缓冲区内容
<?php
$file = 'index.html';
//输出缓存 1分钟过期
if(file_exists($file) && time() < (filemtime($file)+60)){
echo file_get_contents($file);
exit();
}
ob_start(); //开启缓存
$tableData = [
['name'=>'张3','age'=>30],
['name'=>'李4','age'=>40],
['name'=>'王5','age'=>50],
];
?>
<html>
<head>
<title>数据表格</title>
</head>
<body>
<p>刷新时间:<?=date('Y-m-d H:i:s')?></p>
<table>
<tr>
<th>姓名</th><th>年龄</th>
</tr>
<?php foreach ($tableData as $item):?>
<tr>
<td><?=$item['name']?></td><td><?=$item['age']?></td>
</tr>
<?php endforeach;?>
</table>
</body>
</html>
<?php
$out = ob_get_contents();
//保存
file_put_contents($file,$out);
ob_end_flush(); //输出
?>
局部纯静态
<html>
<head>
<title>部分静态</title>
</head>
</html>
<body>
<p>当前时间:<?=date('Y-m-d H:i:s')?></p>
<?php
echo file_get_contents('http://静态文件地址');
?>
</body>
伪静态
.htaccess文件
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule index.html$ index.php
</IfModule>