codetc - 网站开发技术 首页 代码 PHP 查看内容

Discuz 定时任务自动生成sitemap.xml

2015-8-27 22:31| 发布者: CODETC| 查看: 9726| 评论: 5

Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页。最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度为何等),以便搜索引擎可以更加智能地抓取网站。


对于网上很多的 Discuz sitemap插件,个人感觉大部分都很不稳定,总会出现这样那样的问题;由于Discuz 具有定时任务的功能,所以我们要自己实现自动生成sitemap.xml的话也是相应方便的。


由于本站主要是使用Discuz的门户功能,大家可以根据以下思路自由更改,增加论坛或其它部分的sitemap,如果网站内容过多,也可以对sitemap进行拆分;具体实现这里就不详细说了,其实都比较简单


<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

global $sm_step,$portal_url,$portal_page;
$sm_step = 2000;
$portal_page  = "article";
$sitemap_path = "/";

if(!is_dir(DISCUZ_ROOT.$sitemap_path))
{
	echo "{$sitemap_path}不存在,请手工创建目录,并赋予写入权限...<br/>";
	exit;
}

$portal_url=($_G['setting']['domain']['app']['portal'])?($_G['setting']['domain']['app']['portal']):($_G['setting']['domain']['app']['default']?$_G['setting']['domain']['app']['default']:$_SERVER['HTTP_HOST']);

create_portal_sitemap();

function create_portal_sitemap()
{
	global $sm_step,$portal_url,$portal_page;

	$query= DB::query ("SELECT aid, dateline FROM ".DB::table('portal_article_title')." where status = 0 ORDER BY aid DESC LIMIT 0,".$sm_step);
	$portal_sitemap=new sitemap();
	$line=0;
	while ($row = DB::fetch($query))
	{
		$postdate=Date('c',$row['dateline']);
		//该处地址就自行更改
		$loc="http://".$portal_url."/".$portal_page."-".$row['aid']."-1.html";
		$portal_sitemap->AddItem($loc,"daily","0.8",$postdate);
		$line++;
	}
	$portal_sitemap->buildSitemap();
	$portal_sitemap->SaveToFile(DISCUZ_ROOT.$sitemap_path."sitemap.xml");
	unset($portal_sitemap);	
}

 /*
  *类名:sitemap
  *说明:googlemap
  *要求PHP>=5.0
  *使用方法:
  *
  */
class sitemap{
	private $items = array();
	private $content="";
	 /**
	 * 增加节点
	 * @param string $loc  页面永久链接地址
	 * @param date $lastmod  页面最后修改时间,ISO 8601中指定的时间格式进行描述
	 * @param string $changefreq  页面内容更新频率。这里可以用来描述的单词共这几个:"always", "hourly", "daily", "weekly", "monthly", "yearly"
	 * @param string $priority 是用来指定此链接相对于其他链接的优先权比值。此值定于0.0 - 1.0之间
	 * @return  array $items
	 * 用法举例:)
	 */
	public function AddItem($loc,$changefreq,$priority,$lastmod)
	{
		$loc=$this->replacestr($loc);
		$this->items[]= array('loc'=>$loc,
						'changefreq'=>$changefreq,
						'priority'=>$priority,
						'lastmod'=>$lastmod );
	}
	
	//替换loc特殊字符
	public function replacestr($str)
	{
		str_replace("&","&",$str);
		str_replace("'","'",$str);
		str_replace("\"",""",$str);
		str_replace(">",">",$str);
		str_replace("<","<",$str);
		return $str;
	}
	
	//打印显示
	public function Show()
	{
		if (empty($this->content)) 
		$this->buildSitemap();
		echo($this->content);
	}
	
	 /**
	 * 生成sitemap
	 */
	public function buildSitemap()
	{
		$str="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		//$str .="<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.9\">\n";
		$str.="<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ";
		$str.="xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" ";
		$str.="xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">";
		for ($i=0;$i<count($this->items);$i++) 
		{
				$str .= "<url>\n";
				$str .= "<loc>{$this->items[$i]['loc']}</loc>\n";
				$str .= "<lastmod>{$this->items[$i]['lastmod']}</lastmod>\n";
				$str .= "<changefreq>{$this->items[$i]['changefreq']}</changefreq>\n";
				$str .= "<priority>{$this->items[$i]['priority']}</priority>\n";
				$str .="</url>\n";

			}
			$str .= "</urlset>";
			$this->content = $str ;
	}

	/**
	 * 生成sitemap
	 */
	public function buildSitemapIndex()
	{
		$str="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		$str .="<sitemapindex xmlns=\"http://www.google.com/schemas/sitemap/0.9\">\n";
		for ($i=0;$i<count($this->items);$i++) 
		{
			$str .= "<sitemap>\n";
			$str .= "<loc>{$this->items[$i]['loc']}</loc>\n";
			$str .= "<lastmod>{$this->items[$i]['lastmod']}</lastmod>\n";
			$str .="</sitemap>\n";

		}
		$str .= "</sitemapindex >";
		$this->content = $str ;
	}
	
	//保存文件
	public function SaveToFile($filename){
		$handle = fopen($filename, 'wb');
		if ($handle)
		{
			fwrite($handle, $this->content);
			fclose($handle);
		}
		else
		{
			echo ("创建失败");
		}
	}

	//构造函数
	function __construct(){
	}

	 //析构函数
	function __destruct(){
		unset($this->items,$this->content);
	}
}
?>

使用方法 :
1、把以上代码保存为cron_sitemap.php上传至source\include\cron目录
2、进入后台->工具->计划任务->新增


更新周期可自定,建议不要太频繁。
文章来源 CODETC,欢迎分享,转载请注明地址: http://www.codetc.com/article-237-1.html
发表评论

最新评论

引用 游泳  2018-4-5 13:50
感谢codetc,这个sitemap不错。
引用 iqq  2016-11-16 11:47 (待审核)
多谢啦,回去试下。效果在这里  http://iqq.com.au
引用 明海  2016-4-14 14:53
str_replace("\"",""",$str);
这句引号有点问题
引用 游客 2015-9-1 13:42
谢谢分享,很好的文章,网站做的也不错

查看全部评论(5)

 作为游客发表评论,请输入您的昵称

返回顶部