本文最后更新于 1570 天前,其中的信息可能已经有所发展或是发生改变。
按照以下来操作
在网站的根目录下
| <?php |
| $nowtime=time(); |
| $pastsec = $nowtime - $_GET["t"]; |
| if($pastsec<600) |
| { |
| exit; |
| } |
| ob_start(); |
| include("index.php"); |
| $content = ob_get_contents(); |
| $content .= "\n<script language=javascript src=\"jingtai.php?t=".$nowtime."\"></script>"; |
| |
| file_put_contents("index.html",$content); |
| if (!function_exists("file_put_contents")) |
| { |
| function file_put_contents($fn,$fs) |
| { |
| $fp=fopen($fn,"w+"); |
| fputs($fp,$fs); |
| fclose($fp); |
| } |
| } |
| ?> |
并访问这个文件之后会生成一个 index.html 文件并在文件末尾出现 script 标签并附带一个时间戳,当访问同 index.html的时候会加载这个 script 标签并访向一次index.php这个访问时会获取一次当前时间,如果和传递进来的参数相比时间大于设置的600秒就会刷新一次
缺点:如果是将nginx/ Apache / caddy 的默认页面设置为html优先,通过首页点击文章页面无法正常跳转,无法配合 Handsome 的pjax
来自 SunPma
https://sunpma.com/484.html
最近感觉博客打开速度慢了不少,测试来看均值基本都在3秒以上,因为博客上有不少的 CSS
和 JS
加载速度也就慢了下来,并且本博客没有做图站分离,都是放在一起的,速度确实较慢;
于是给首页做了个静态 HTML
,首页打开速度确实提升了数倍,需要的朋友也可以试试;
未做静态HTML前的打开测试
生成静态HTML后的打开测试
对比可以看出提升确实比较大,从均值3.5秒提升到了1秒以内,相当满意了;
这里提供两个 PHP
代码脚本,一个是不需要密码的,一个是需要密码的;
首先在网站根目录新建一个 php
文件,文件命名为:jingtai.php
然后在其中写入需要的代码(不需要密码和需要密码的选择其中一个即可)
不需要密码的PHP脚本
| <?php |
| $nowtime=time(); |
| $pastsec = $nowtime - $_GET["t"]; |
| if($pastsec<600) |
| { |
| exit; |
| } |
| ob_start(); |
| include("index.php"); |
| $content = ob_get_contents(); |
| $content .= "\n<script language=javascript src=\"jingtai.php?t=".$nowtime."\"></script>"; |
| |
| file_put_contents("index.html",$content); |
| if (!function_exists("file_put_contents")) |
| { |
| function file_put_contents($fn,$fs) |
| { |
| $fp=fopen($fn,"w+"); |
| fputs($fp,$fs); |
| fclose($fp); |
| } |
| } |
| ?> |
需要要密码的PHP脚本
| <?php |
| |
| |
| |
| ini_set( 'date.timezone', 'PRC' ); |
| |
| |
| $expire = 600; |
| |
| $password = '123456'; |
| $file_time = @filemtime( 'index.html' ); |
| time() - $file_time > $expire && create_index(); |
| isset( $_GET['password'] ) && $_GET['password'] == $password && create_index(); |
| |
| |
| |
| |
| function create_index() |
| { |
| ob_start(); |
| include( 'index.php' ); |
| $content = ob_get_contents(); |
| $content .= "\n<!-- Create time: " . date( 'Y-m-d H:i:s' ) . " -->"; |
| |
| $content .= "\n<script language=javascript src='jingtai.php'></script>"; |
| ob_clean(); |
| $res = file_put_contents( 'index.html', $content ); |
| if ( $res !== false ) |
| { |
| die( 'Create successful' ); |
| } |
| else |
| { |
| die( 'Create error' ); |
| } |
| } |
保存为 utf8
编码后退出;
注意:如果使用的宝塔面板,还要在后台网站设置-默认文档-将 index.html
的排序上调至第一位
然后在浏览器中打开 PHP
脚本链接即可
打开链接后会在网站根目录下生成一个 index.html
的静态文件,首页静态化也就完成了;
不需要密码脚本的链接:https://你的域名/jingtai.php
需要密码脚本的链接:https://你的域名/jingtai.php?password=123456
两个 PHP
脚本中的更新时间默认为 600
秒,也就是十分钟更新一次,默认密码为 123456
更新时间及访问密码都可以自行设定,修改脚本中的数值即可;
查看是否生效,浏览器中重新打开你的网站首页,右键查看源代码
不需要密码的脚本
在最后一行如果有 <script language=javascript>...
之类的字眼,说明你访问的就是index.html的页面
需要密码的脚本
在最后一行会有显示最后一次更新时间
<!-- Create time: 2020-03-09 00:33:41 -->
<script language=javascript src='jt_index.php'></script>
当然也可以测试工具进行网站打开测试;
网站测试:https://www.boce.com/tool