网页全站静态化
<?php
if(!function_exists('putfile')){
    function putfile($file) {
        $stat = @stat ( $file );
        $etag = sprintf ( '%x-%x-%x', $stat ['ino'], $stat ['size'], $stat ['mtime'] * 1000000 );
        if (isset ( $_SERVER ['HTTP_IF_NONE_MATCH'] ) && $_SERVER ['HTTP_IF_NONE_MATCH'] == $etag) { // 火狐浏览器为HTTP_IF_NONE_MATCH
            header ( 'Etag:' . $etag, true, 304 );
            exit ();
        } elseif (isset ( $_SERVER ['HTTP_IF_MODIFIED_SINCE'] ) && strtotime ( $_SERVER ['HTTP_IF_MODIFIED_SINCE'] ) >= $stat ['mtime']) { // IE浏览器为HTTP_IF_MODIFIED_SINCE,$stat['mtime']文件修改时间
            // header('Last-Modified: ' . date('r', $stat['mtime']));
            header ( 'Etag:' . $etag, true, 304 );
            exit ();
        }
        header ( "Content-type:text/html;charset=utf-8" );
        header ( 'Last-Modified: ' . date ( 'r', $stat ['mtime'] ) );
        header ( 'Etag: "' . $etag . '"' );
        if (readfile ( $file ) === false) {
            header ( 'HTTP/1.0 500 Internal Server Error' );
        }
    }
}
if(!function_exists('cache_info')) {
    function cache_info($file, $dir = '')
    {
        $file = $dir ? DT_CACHE . '/' . $dir . '/' . $file : DT_CACHE . '/' . $file;
        if (!is_file($file)) return '';
        $info = [
            'filepath' => $file,
            'filesize' => filesize($file),
            'filectime' => filectime($file),
            'filemtime' => filemtime($file),
            'fileatime' => fileatime($file),
        ];
        return $info;
    }
}
/**
 * 缓存生成定义,有缓存且未过期,直接输出
 * @param $url
 */
function lyf_cachehtml_begin($url){
    global $CFG;
    if(empty($url) || strpos($url,'?')!==false)return;
    $cache_html_flag = 0;
    $cache_html_intvaltime = 180;
    $cache_pdir = 'cachehtml';
    $cache_split = '/';
    if(!DT_DEBUG){//非调试模式
        if(defined('DT_WAP')){//M端缓存
            $cache_html_flag = 1;
        }
        if($cache_html_flag){
            $cache_urls = pathinfo($url);
            $cache_file = str_replace(['.shtml','.html','.htm'],'',$cache_urls['basename']).'.html';
            $cache_urlinfos = explode('/',str_replace(get_env('scheme').$_SERVER['HTTP_HOST'],'',$url));
            $cache_urlinfos = array_filter($cache_urlinfos,function ($v){
                return !empty($v) && strpos($v,'.')===false ;
            });
            $cache_host = get_env('host');
            $cache_domain = str_replace($CFG['cookie_domain'], '', $cache_host);
            $cache_dir = $cache_pdir.$cache_split.$cache_domain.$cache_split.join($cache_split,$cache_urlinfos);
            $cache_info = cache_info($cache_file,$cache_dir);
            $filepath = DT_CACHE.$cache_split.$cache_dir.$cache_split.$cache_file;
            if(empty($cache_info) || time() - $cache_info['filemtime'] > $cache_html_intvaltime){//生成缓存
                defined('DT_CACHEHTM') or define('DT_CACHEHTM', true);
                defined('DT_CACHEHTM_PATH') or define('DT_CACHEHTM_PATH', $filepath);
                ob_start();
            }else{
                if(is_file($filepath) ) {
                    putfile($filepath);
                    exit();
                }
            }
        }
    }
}
function lyf_cachehtml_end(){
    if(defined('DT_CACHEHTM') && defined('DT_CACHEHTM_PATH')){
        $data = ob_get_contents();
        //ob_clean();
        file_put(DT_CACHEHTM_PATH, $data);
        ob_end_flush();
    }
}
//生成缓存
lyf_cachehtml_begin($DT_URL);

                
            
        
浙公网安备 33010602011771号