织梦DedeCMS首页定时自动生成HTML

最近新建一个站,用的全站伪静态,不知道是不是首页调用数据太多,还是咋地,就是动态访问首页特卡,得十几秒才能打开。但首页生成html文件后,访问正常。每天登录后台手动生成html是不现实的,所以就想把它设置为定时自动生成html,经测试后,此方法了正好解决了我的问题。

第一步

写一个触发定时自动更新的php文件,新建一个php文件,内容如下,标题为autoindex.php,把它放到plus文件夹中。

<?php
function sp_input( $text )
{
$text = trim( $text );
$text = htmlspecialchars( $text );
if (!get_magic_quotes_gpc())
return addslashes( $text );
else
return $text;
}
$autotime = 120;//自动更新时间,单位为秒
$fpath = "../data/last_time.inc";//记录更新时间文件,如果不能达到目的,请检查是否有读取权限。
include( $fpath );
if( empty($last_time))
$last_time = 0;
if( sp_input($_GET['renew'])=="now")
$last_time = 0;
if((time()-$last_time)>=$autotime )
{
define('DEDEADMIN', ereg_replace("[/\\]{1,}",'/',dirname(__FILE__) ) );
require_once(DEDEADMIN."/../include/common.inc.php");
require_once(DEDEINC."/arc.partview.class.php");
/*
$row = $dsql->GetOne("Select * From dede_homepageset");
$dsql->Close();
$templet=$row['templet'];
$position=$row['position'];
*/
$templet = “default/index.htm”;//这里是首页模板位置,当前是dede默认首页位置。
$position = "../index.html";
$homeFile = dirname(__FILE__)."/".$position;
$homeFile = str_replace("\\", "/", $homeFile );
$homeFile = str_replace( "//", "/", $homeFile );
$pv = new PartView();
$pv ->SetTemplet( $cfg_basedir.$cfg_templets_dir."/".$templet );
$pv -> SaveToHtml( $homeFile );
$pv -> Close();
$file = fopen( $fpath, "w");
fwrite( $file, "<?php\n");
fwrite( $file,"\$last_time=".time().";\n");
fwrite( $file, '?>' );
fclose( $file );
}
?>

第二步

在首页的模版代码head标签中引入触发文件代码:

<script src="/plus/autoindex.php" type="text/javascript"></script>

然后手动更新一下首页,接下来在设置的时间过了以后如果有用户访问首页就会触发自动更新文件,首页就会自动更新一次。

THE END