我正在寻找一个脚本在运行时从动态内容生成静态HTML页面。
我想做的就是保存这些缓存这些html生成的页面以供离线浏览
谁能告诉我正确的方向?谢谢
如果您想手动执行此操作,可以使用输出缓冲。例如:
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]
);
使用fopen并保存页面以供离线阅读,但它比听起来要复杂得多