Generate HTML Static Pages from Dynamic Php Pages

后端 未结 3 1510
终归单人心
终归单人心 2021-02-10 11:46

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 fo

相关标签:
3条回答
  • 2021-02-10 12:06

    Check this out for the basics: http://www.theukwebdesigncompany.com/articles/php-caching.php Also, check out APC http://php.net/manual/en/book.apc.php And Turk MMCache: http://turck-mmcache.sourceforge.net/

    0 讨论(0)
  • 2021-02-10 12:10

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

    0 讨论(0)
  • 2021-02-10 12:14

    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]
    );
    
    0 讨论(0)
提交回复
热议问题