[英]Generate HTML Static Pages from Dynamic Php Pages
我正在尋找一個腳本,以便在運行時從動態內容生成靜態HTML頁面。
我基本上想要做的是保存那些用於離線瀏覽的html生成頁面的緩存。
有誰能請我指出正確的方向?
謝謝
如果要手動執行此操作,可以使用輸出緩沖。 例如:
文件static.php
:
Hello, <a href="profile.php"><?php echo htmlspecialchars($username); ?></a>!
文件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);
}
文件index.php
:
require_once 'functions.php';
cache_page(
'static.php',
getUser()->id,
['username' => getUser()->username]
);
查看基礎知識: http : //www.theukwebdesigncompany.com/articles/php-caching.php另外,請查看APC http://php.net/manual/en/book.apc.php和Turk MMCache: http://turck-mmcache.sourceforge.net/
使用fopen並保存頁面以進行離線閱讀,但聽起來很簡單
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.