因为对帝国CMS的sitemap插件不太满意,所以写了一个sitemap生成工具,把首页链接、栏目页链接、内容页链接放在sitemap里,然后生成一个XML文件提交给百度站长工具。本文中的代码需要生动地生成。我自己在Linux下用cron写了一个定时任务,每天固定时间生成一次,先这样吧~
代码如下:
lt;?服务器端编程语言(Professional Hypertext Preprocessor的缩写)
/*参考文件*/
要求(# 39;e/class/connect . PHP # 39;);
要求(# 39;e/class/db _ SQL . PHP # 39;);
要求(# 39;e/data/db cache/class . PHP # 39;);lt;/pgt;lt;pgt/*建立数据库链接并实例化类*/
$ link = db _ connect();
$ empire = new MySQL query();lt;/pgt;lt;pgt//header(quot;content -类型:application/XML quot;);
标题(quotcontent -类型:文本/html;charset = ut F-8 quot;);lt;/pgt;lt;pgt$ operation = $ _ GET[# 39;操作# 39;];
$ current _ URL = explode(# 39;/#39;,$ _ SERVER[# 39;PHP _ SELF # 39]);
$ current _ script _ name = end($ current _ URL);lt;/pgt;lt;pgtif($运算放大器;amp$ operation = = quotmakexmlquot)
{
$ XML = generate _ sitemp _ XML();
$ result = save _ XML(# 39;sitemap.xml # 39,$ XML);
如果($结果)
{
回声# 39;生成成功,lt;a href = quotsitemap.xmlquotgt;单击此处查看lt;/AGT;#39;;
}
其他
{
回声# 39;生成失败,lt;a href = quot#39;。$current_script_name#39;quotgt;再来lt;/AGT;!#39;;
}
}
其他
{
回声# 39;欢迎使用微笑鱼网站地图生成工具,请lt;a href = quot#39;。$current_script_name#39;?operation = makexmlquotgt;单击此处生成lt;/AGT;!#39;;
} lt/pgt;lt;pgtlt;/pgt;lt;pgt
//保存到文件
函数save_xml($filename,$text) {
如果(!$filename ||!$text)
返回false
@ chmod($文件名,0777);
if ($fp = fopen($filename,quotwquot)) {
if (@fwrite($fp,$text)) {
fclose($ FP);
返回true
}否则{
fclose($ FP);
返回false
}
}
返回false
}
//生成站点地图XML数据
函数generate_sitemp_xml()
{
global $dbtbpre,$ empire
$ xml = # 39#39;;
$xml。= #39;lt;?xml版本= quot1.0quot编码= quotut F-8 quot;?gt;#39;。PHP _ EOL
$xml。= #39;lt;urlsetgt。#39;。PHP _ EOL
$xml。= generate _ home _ XML();//主页
$xml。= generate _ class _ XML();//列
$ query = quotselect * from {$dbtbpre } ecms _ news order by id desc限制100quot;
$ SQL = $ empire -gt;查询($ query);
while($ r = $ empire -gt;fetch($sql))
{
$ title URL = sys _ ReturnBqTitleLink($ r);
$xml。= #39;lt;urlgt。#39;。PHP _ EOL
$xml。= #39;lt;locgt#39;。$titleurl。#39;lt;/locgt。#39;。PHP _ EOL
$xml。= #39;lt;lastmodgt#39;。strftime(# 39;% Y-% m-% d # 39;,$ r[# 39;新闻时间# 39;]).#39;lt;/lastmodgt;#39;。PHP _ EOL
$xml。= #39;lt;changefreqgt。dailylt/changefreqgt。#39;。PHP _ EOL
$xml。= #39;lt;优先级gt。0.8lt/prioritygt。#39;。PHP _ EOL
$xml。= #39;lt;/urlgt。#39;。PHP _ EOL
}
$xml。= #39;lt;/urlsetgt。#39;。PHP _ EOL
返回$ xml
}
//生成列链接XML数据
函数generate_class_xml()
{
global $dbtbpre,$empire,$ class _ r;
$ xml = # 39#39;;
$ SQL = $ empire -gt;查询(quotSELECT * FROM { $ dbtbpre } enews class其中islast = 1quot);
while($ r = $ empire -gt;fetch($sql))
{
$ infor = $ empire -gt;fetch 1(quot;从{ $ dbtbpre } ecms _ quot中选择新闻时间;。$class_r[$r[classid]][tbname]。quot在哪里
classid = # 39$ r[classid]# 39;按新闻时间DESC限制1quot);
$ class _ URL = sys _ returnbqclass name($ r,9);
$xml。= #39;lt;urlgt。#39;。PHP _ EOL
$xml。= #39;lt;locgt#39;。$class_url。#39;lt;/locgt。#39;。PHP _ EOL
$xml。= #39;lt;lastmodgt#39;。strftime(# 39;% Y-% m-% d # 39;,时间())。#39;lt;/lastmodgt;#39;。PHP _ EOL
$xml。= #39;lt;changefreqgt。dailylt/changefreqgt。#39;。PHP _ EOL
$xml。= #39;lt;优先级gt。0.8lt/prioritygt。#39;。PHP _ EOL
$xml。= #39;lt;/urlgt。#39;。PHP _ EOL
}
返回$ xml
}
//生成列链接XML数据
函数generate_home_xml()
{
$ xml = # 39#39;;
$xml。= #39;lt;urlgt。#39;。PHP _ EOL
$xml。= #39;lt;locgthttps://www . jb51 . net lt;/locgt。#39;。PHP _ EOL
$xml。= #39;lt;lastmodgt#39;。strftime(# 39;% Y-% m-% d # 39;,时间())。#39;lt;/lastmodgt;#39;。PHP _ EOL
$xml。= #39;lt;changefreqgt。dailylt/changefreqgt。#39;。PHP _ EOL
$xml。= #39;lt;优先级gt。0.8lt/prioritygt。#39;。PHP _ EOL
$xml。= #39;lt;/urlgt。#39;。PHP _ EOL
返回$ xmllt;/pgt;lt;pgt} lt/pgt;lt;pgt/*关闭数据库连接并释放该类*/
db _ close();
$ empire = null
?登录后复制
