Typecho首页静态化,提高访问速度

PHP
技之树 2022-10-25

859 0

用法

  1. 新建文件1ove.php

  2. 填入代码

  3. 脚本自带更新,但也可以使用宝塔定时任务,访问 http://yousite.com/1ove.php

    不过如果使用定时任务,需要将代码中的js代码注释掉。

  4. 并将默认文件中的index.html放在首位

代码

无密码参数

<?php
$nowtime=time();
$pastsec = $nowtime - $_GET["t"];
if($pastsec<600)
{
exit; //10分钟更新一次,时间可以自己调整
}
ob_start(); //打开缓冲区
include("index.php");
$content = ob_get_contents(); //得到缓冲区的内容
$content .= "\n<script language=javascript src=\"1ove.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
/**
 * 首页静态化脚本
 */
ini_set( 'date.timezone', 'PRC' );

/* 缓存过期时间 单位:秒 */
$expire = 600;
/* 主动刷新密码  格式:https://你的域名/1ove.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='1ove.php'></script>";
    ob_clean();
    $res = file_put_contents( 'index.html', $content );
    if ( $res !== false )
    {
        die( 'Create successful' );
    }
    else
    {
        die( 'Create error' );
    }
}
这家伙太懒了,什么也没留下。
最新回复 (0)
    • YiOVE论坛
      2
         
返回