因为对帝国cms的sitemap插件不太满意,所以自己写了个sitemap生成工具,在sitemap中放了首页链接、栏目页链接和内容页链接,然后生成xml文件并且提交到百度站长工具,本文中的代码是需要生动生成的,我自己是在linux中用cron写的定时任务,每天固定时间生成一次,先这样吧~
代码如下:
/*建立数据库链接 与 实例化类*/ $link = db_connect(); $empire=new mysqlquery();
//header("Content-type:application/xml"); header("Content-type: text/html; charset=utf-8");
$operation = $_GET['operation']; $current_url = explode('/',$_SERVER['PHP_SELF']); $current_script_name = end($current_url);
if($operation && $operation == "makexml") { $xml = generate_sitemp_xml(); $result = save_xml('sitemap.xml',$xml); if($result) { echo '生成成功,点此查看'; } else { echo '生成失败,在来一次!'; } } else { echo '欢迎使用微笑的鱼Sitemap生成工具,请点此生成!'; }
//保存到文件 function save_xml($filename, $text) { if (!$filename || !$text) return false; @chmod($filename,0777); if ($fp = fopen($filename, "w")) { if (@fwrite($fp, $text)) { fclose($fp); return true; } else { fclose($fp); return false; } } return false; } //生成Sitemap XML数据 function generate_sitemp_xml() { global $dbtbpre,$empire; $xml = ''; $xml .= '' .PHP_EOL; $xml .= '
' .PHP_EOL; $xml .= generate_home_xml();//首页 $xml .= generate_class_xml();//栏目 $query="select * from {$dbtbpre}ecms_news order by id desc limit 100"; $sql=$empire->query($query); while($r=$empire->fetch($sql)) { $titleurl=sys_ReturnBqTitleLink($r); $xml .= ' ' .PHP_EOL; return $xml; } //生成栏目链接XML数据 function generate_class_xml() { global $dbtbpre,$empire,$class_r; $xml = ''; $sql=$empire->query("SELECT * FROM {$dbtbpre}enewsclass WHERE islast=1"); while($r=$empire->fetch($sql)) { $infor=$empire->fetch1("SELECT newstime FROM {$dbtbpre}ecms_".$class_r[$r[classid]][tbname]." WHERE classid='$r[classid]' ORDER BY newstime DESC LIMIT 1"); $class_url=sys_ReturnBqClassname($r, 9); $xml .= '' .PHP_EOL; $xml .= ' ' .PHP_EOL; } $xml .= ''.$titleurl.' ' .PHP_EOL; $xml .= ''.strftime('%Y-%m-%d',$r['newstime']).' ' .PHP_EOL; $xml .= 'daily ' .PHP_EOL; $xml .= '0.8 ' .PHP_EOL; $xml .= '' .PHP_EOL; $xml .= ' ' .PHP_EOL; } return $xml; } //生成栏目链接XML数据 function generate_home_xml() { $xml = ''; $xml .= ''.$class_url.' ' .PHP_EOL; $xml .= ''.strftime('%Y-%m-%d',time()).' ' .PHP_EOL; $xml .= 'daily ' .PHP_EOL; $xml .= '0.8 ' .PHP_EOL; $xml .= '' .PHP_EOL; $xml .= ' ' .PHP_EOL; return $xml;https://www.jb51.net ' .PHP_EOL; $xml .= ''.strftime('%Y-%m-%d',time()).' ' .PHP_EOL; $xml .= 'daily ' .PHP_EOL; $xml .= '0.8 ' .PHP_EOL; $xml .= '}
/*关闭数据库连接 与 释放类*/ db_close(); $empire=null; ?
推荐学习《帝国cms教程》
以上就是经典帝国CMS生成sitemap实现代码分享的详细内容,更多请关注本网内其它相关文章!