Typecho生成首页静态HTML密码版

低调G
2020-05-30 / 0 评论 / 1,497 阅读 / 正在检测是否收录...

介绍

之前已经发过Typecho首页生成index.html
静态文件,这次是带密码版本。

开始

首先在网站根目录新建一个文件名为f5.php的PHP文件。
然后在文件中写入如下代码:

<?php
/**
 * 首页静态化脚本
 */
ini_set( 'date.timezone', 'PRC' );
/* 缓存过期时间 单位:秒 */
$expire = 600;
/* 主动刷新密码  格式:https://你的域名/f5.php?password=123456 */
$password = '123456';
$file_time = @filemtime( 'index.html' );
time() - $file_time > $expire && create_index();
isset( $_GET['password'] ) && $_GET['password'] == $password && create_index();
/**
 * 生成 index.html
 */
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='f5.php'></script>";
    ob_clean();
    $res = file_put_contents( 'index.html', $content );
    if ( $res !== false )
    {
        die( '创建成功!' );
    }
    else
    {
        die( '创建失败!' );
    }
}

保存为utf8编码后退出。
在浏览器中打开PHP脚本链接后会在网站根目录下生成一个index.html的静态文件,首页静态化也就完成了。
脚本链接地址:https://你的域名/f5.php?password=123456

说明

脚本中的更新时间默认为600秒,也就是十分钟更新一次,默认密码为123456
更新时间及访问密码都可以自行设定,修改脚本中的数值即可。

验证

在浏览器中重新打开你的网站首页,右键查看源代码,成功的话会在最后一行显示最后一次更新时间
<!-- Create time: 2020-05-09 11:20:30 -->
<script language=javascript src='f5.php'></script>
提示:记得将index.html排序放在网页打开首位。

1

评论 (0)

取消