TypechoJoeTheme

至尊技术网

统计
登录
用户名
密码
/
注册
用户名
邮箱

悠悠楠杉

网站页面

PHP将动态网页生成HTML纯静态网页

2020-12-15
/
0 评论
/
658 阅读
/
正在检测是否收录...
12/15
<?php
start(array(
    'index_file' => __DIR__ . '/2.php', //你首页的文件名
    'cache_file' => __DIR__ . '/cache/index/', //要缓存的路径
    'cache_index_file' => 'index.html', //要缓存的文件名
    'expire_seconds' => 43200, //过期的秒数(60秒=1分钟)
));
function start($config)
{
    $remain_seconds = $file_timestamp = 0;
    if (hasCached($config, $remain_seconds, $file_timestamp)) {
        $html = '';
        $html .= file_get_contents($config['cache_file'] . $config['cache_index_file']);
        die($html);
    }
    ob_start();
    require $config['index_file'];
    $content = ob_get_contents();
    file_put_contents($config['cache_file'] . $config['cache_index_file'], $content);
}

function hasCached($config, &$remain_seconds, &$file_timestamp)
{
    if (!file_exists($config['cache_file'] . $config['cache_index_file'])) {
        if (!is_dir($config['cache_file'])) {
            mkdir($config['cache_file'], 0777, true); //创建目录/cache/index/
        }
        fopen($config['cache_file'] . $config['cache_index_file'], 'w'); // 创建文件index.html
        return false;
    }
    if (filesize($config['cache_file'] . $config['cache_index_file']) === 0) {
        return false;
    }
    $file_timestamp = filemtime($config['cache_file'] . $config['cache_index_file']);
    $remain_seconds = $config['expire_seconds'] - (time() - $file_timestamp);
    if ($remain_seconds <= 0) {
        return false;
    }
    return true;
}

经验PHP动态网页静态网页
朗读
赞(0)
版权属于:

至尊技术网

本文链接:

https://www.zzwws.cn/archives/4973/(转载时请注明本文出处及文章链接)

评论 (0)