Creating Static HTML Pages from Dynamic Php Pages

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

Basically I want to save these cached html pages for offline viewing.

Can someone point me in the right direction?

thank

+4
source share
3 answers

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

Example:

static.php:

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

functions.php:

/**
 * Prints/load cached page.
 * @param string $template The dynamic page to cache.
 * @param integer $uid The user ID (security precaution to prevent collissions).
 * @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(){
        // set the value of a variable.
        foreach($vars as $name=>$value)$$name=$value;
        // start output buffering and render page
        ob_start();
        include_once $template;
        // end output buffering and save data to cache file
        file_put_contents($cache,ob_get_clean());
    }
    readfile($cache);
}

index.php:

require_once 'functions.php';

cache_page(
    'static.php',
    getUser()->id,
    array('username'=>getUser()->username)
);
+3

fopen , tricker tan

0

All Articles