开发者

Generate HTML Static Pages from Dynamic Php Pages [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 11 months ago.

Improve this question

I am looking for a script to generate static HTML pages from dynamic content at runtime.

What I basically want to do is to save those cache those html generated pages for offline browsing.

Could anyone please point me in the right direction?

开发者_StackOverflow

Thanks


If you want to do this manually, you can use output buffering. For example:

File static.php:

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

File 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:

require_once 'functions.php';

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


Use fopen and save the page for offline reading, but its a lot tricker tan it sounds

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜