简体   繁体   English

从动态Php页面生成HTML静态页面

[英]Generate HTML Static Pages from Dynamic Php Pages

I am looking for a script to generate static HTML pages from dynamic content at runtime. 我正在寻找一个脚本,以便在运行时从动态内容生成静态HTML页面。

What I basically want to do is to save those cache those html generated pages for offline browsing. 我基本上想要做的是保存那些用于离线浏览的html生成页面的缓存。

Could anyone please point me in the right direction? 有谁能请我指出正确的方向?

Thanks 谢谢

If you want to do this manually, you can use output buffering. 如果要手动执行此操作,可以使用输出缓冲。 For example: 例如:

File static.php : 文件static.php

Hello, <a href="profile.php"><?php echo htmlspecialchars($username); ?></a>!

File functions.php : 文件functions.php

/**
 * Renders cached page.
 *
 * @param string $template The dynamic page to cache.
 * @param integer $uid The user ID (security precaution to prevent collisions).
 * @param array $vars Set of variables passed to dynamic page.
 */
function cache_page($template, $uid, $vars)
{
    $cache = 'cache/' . $uid . '-' . md5(@json_encode($vars)) . '.cache';

    if (!file_exists($cache)) { // <- also maybe check creation time?
        // set up template variables
        extract($template_vars, EXTR_SKIP);

        // start output buffering and render page
        ob_start();
        include $template;

        // end output buffering and save data to cache file
        file_put_contents($cache, ob_get_clean());
    }

    readfile($cache);
}

File index.php : 文件index.php

require_once 'functions.php';

cache_page(
    'static.php',
    getUser()->id,
    ['username' => getUser()->username]
);

使用fopen并保存页面以进行离线阅读,但听起来很简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM