隔时生成首页的PHP代码:
set_time_limit(0);
$baseCmsUrl = "http://www.studstu.com";//你网站的根网址,结束不要加 /
$dmPageName = "index.php";//动态主页的名称
$stPageName = "index.html";//生成静态主页的名称
$mkTime = 3600;//你希望多长时间更新一次,单位是秒
//下面是执行的代码
$tureStFile = dirname(__FILE__).'/'.$stPageName;
$ftime = @filemtime($tureStFile);
if(!file_exists($tureStFile) || ($ftime < time()-$mkTime))
{
$body = file_get_contents($baseCmsUrl.'/'.$dmPageName);
$fp = fopen($tureStFile, 'w');
fwrite($fp, $body);
fclose($fp);
}
?>
将上面代码保存为task.php
在经常访问的页面调用:
在某个固定的时间执行的PHP代码:
//计划执行时间
if($job=='cron'){
check_method();
//查询数据库的下一个时间是否小于现在的时间
if($maxrecord['deltemptime']
$crondb = array();
//day为0为每日 week周 minute时间断 hour什么时侯开始
$rt['cron'] = array('day'=>'0','week'=>$config['cron_week'],'m'=>$config['cron_minute'],'hour'=>$config['cron_hour']);
$crondb[] = $rt;
}
foreach ($crondb AS $key => $cron_val){
//设置了允许分钟段
if (!empty($config['cron_minute'])){
$m = explode(' ', $config['cron_minute']);
$m_now = intval(local_date('i',$nowtime['timestamp']));
if (!in_array($m_now, $m)) {//不包含此分钟的跳出
continue;
}
}
/*执行任务*/
/*********此处是你写的任务***********************/
$next_time = get_next_time($cron_val['cron']);
$DB->query("UPDATE `maxrec` SET `deltemptime`={$next_time}");
}
exit;
}
?>