php自动sitemap,php开源系统:dedecms伪静态自动sitemap生成器代码

php开源系统:dedecms自动sitemap生成器代码

rewrite ^/sitemap.xml$ /sitemap.php last

/*

适用于dedecms系统,自动sitemap

*/

include("data/common.inc.php");

/*设置*/

$moshi = 'txt'; //xml 或者 html ,txt

$cache = 1; //是否开启缓存

$cachetime = 30; //是分钟

$cachefile = "sitemapcache1"; //如果多个地图模式,每个文件要改下这个文件名

//end

if($cache && file_exists($cachefile.$moshi) && (filemtime($cachefile.$moshi)+$cachetime*60)>time()){

if($moshi=='xml'){

header('Content-type:text/xml');

echo file_get_contents($cachefile.$moshi);

}elseif($moshi=='txt'){

header("content-type: text/plain; charset=utf-8");

echo file_get_contents($cachefile.$moshi);

}else{

include($cachefile.$moshi);

}

exit;

}

$pdo = null;

try{

$pdo = new PDO("mysql:dbname=".$cfg_dbname.";host=".$cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);

}catch(Exception $e){

echo $e->getMessage();

}

$host = "http://".$_SERVER['HTTP_HOST'];

$sql = "SELECT t.id,t.typename,t.typedir,t.namerule FROM dede_arctype t";

$stmt = $pdo->prepare($sql);

$stmt->execute();

$cat = $urlrule = array();

while ($row=$stmt->fetch()){

$typedir[$row['id']] = $row['typedir'];

$namerule[$row['id']] = $row['namerule'];

}

$sql = "SELECT a.id,a.pubdate,a.typeid,a.title FROM dede_archives a order by a.id desc limit 5000";

$stmt = $pdo->prepare($sql);

$stmt->execute();

if($moshi=='xml'){

header('Content-type:text/xml');

$xml = "<?xml version='1.0' encoding='UTF-8'?>";

while ($row=$stmt->fetch())

{

$url = $namerule[$row['typeid']];

$_typedir = str_replace("{cmspath}/","",$typedir[$row['typeid']]);

//{typedir}/{Y}/{M}{D}/{aid}.html

//$url .= "/".date("Y/md",$row['pubdate'])."/".$row['id'].".html";

$url = $host."/".$url;

$url = str_replace("{typedir}",$_typedir,$url);

$url = str_replace("{Y}",date("Y",$row['pubdate']),$url);

$url = str_replace("{M}",date("m",$row['pubdate']),$url);

$url = str_replace("{D}",date("d",$row['pubdate']),$url);

$url = str_replace("{aid}",$row['id'],$url);

$xml .="

".$url."

".date("Y-m-d",$row['pubdate'])."

daily

0.8

";

}

$xml .= "";

echo $xml;

}elseif($moshi=='txt'){

$xml = "";

header("content-type: text/plain; charset=utf-8");

while ($row=$stmt->fetch())

{

$url = $namerule[$row['typeid']];

$_typedir = str_replace("{cmspath}/","",$typedir[$row['typeid']]);

//{typedir}/{Y}/{M}{D}/{aid}.html

//$url .= "/".date("Y/md",$row['pubdate'])."/".$row['id'].".html";

$url = $host."/".$url;

$url = str_replace("{typedir}",$_typedir,$url);

$url = str_replace("{Y}",date("Y",$row['pubdate']),$url);

$url = str_replace("{M}",date("m",$row['pubdate']),$url);

$url = str_replace("{D}",date("d",$row['pubdate']),$url);

$url = str_replace("{aid}",$row['id'],$url);

$xml .=$url."\n";

}

$xml .= "";

echo $xml;

}else{

$xml = "

  • ";

while ($row=$stmt->fetch())

{

$url = $namerule[$row['typeid']];

$_typedir = str_replace("{cmspath}/","",$typedir[$row['typeid']]);

//{typedir}/{Y}/{M}{D}/{aid}.html

//$url .= "/".date("Y/md",$row['pubdate'])."/".$row['id'].".html";

$url = $host."/".$url;

$url = str_replace("{typedir}",$_typedir,$url);

$url = str_replace("{Y}",date("Y",$row['pubdate']),$url);

$url = str_replace("{M}",date("m",$row['pubdate']),$url);

$url = str_replace("{D}",date("d",$row['pubdate']),$url);

$url = str_replace("{aid}",$row['id'],$url);

$xml .="

".$row['title']."    ".date("Y-m-d",$row['pubdate'])."";

}

$xml .= "

";

echo $xml;

}

if($cache){

file_put_contents($cachefile.$moshi,$xml);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值