从动态Php页面生成HTML静态页面
我正在寻找一个脚本,在运行时从动态内容生成静态HTML页面 我基本上想做的是将这些缓存和html生成的页面保存起来,以便脱机浏览 谁能给我指一下正确的方向吗从动态Php页面生成HTML静态页面,php,html,Php,Html,我正在寻找一个脚本,在运行时从动态内容生成静态HTML页面 我基本上想做的是将这些缓存和html生成的页面保存起来,以便脱机浏览 谁能给我指一下正确的方向吗 谢谢使用fopen并将页面保存为离线阅读,但这听起来很棘手请查看以下基本信息: 还有,看看APC 和Turk MMCache:如果要手动执行此操作,可以使用输出缓冲。例如: 文件static.php: /** * Renders cached page. * * @param string $template The dynamic
谢谢使用fopen并将页面保存为离线阅读,但这听起来很棘手请查看以下基本信息: 还有,看看APC
和Turk MMCache:如果要手动执行此操作,可以使用输出缓冲。例如: 文件
static.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);
}
require_once 'functions.php';
cache_page(
'static.php',
getUser()->id,
['username' => getUser()->username]
);
你好!
文件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);
}
require_once 'functions.php';
cache_page(
'static.php',
getUser()->id,
['username' => getUser()->username]
);
任何缓存类都会这样做。您只需要调整它们以使用可读的输出文件名。(或者任何你想问但在问题中没有提到的东西。)