|
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}不存在,请手工创建目录,并赋予写入权限...";
- exit;
- }
- $portal_url=($_G['setting']['domain']['app']['portal'])?($_G['setting']['domain']['app']['portal'])<img src="static/image/smiley/default/sad.gif" border="0" smilieid="2" alt=":(">$_G['setting']['domain']['app']['default']?$_G['setting']['domain']['app']['default']<img src="static/image/smiley/default/shy.gif" border="0" smilieid="8" alt=":$">_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"?>
- ";
- //$str .="<urlset xmlns="http://www.google.com/schemas/sitemap/0.9">
- ";
- $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 <a href="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" target="_blank">[url]http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd</a>[/url]">";
- for ($i=0;$i<count($this->items);$i++)
- {
- $str .= "<url>
- ";
- $str .= "<loc>{$this->items[$i]['loc']}</loc>
- ";
- $str .= "<lastmod>{$this->items[$i]['lastmod']}</lastmod>
- ";
- $str .= "<changefreq>{$this->items[$i]['changefreq']}</changefreq>
- ";
- $str .= "<priority>{$this->items[$i]['priority']}</priority>
- ";
- $str .="</url>
- ";
- }
- $str .= "</urlset>";
- $this->content = $str ;
- }
- /**
- * 生成sitemap
- */
- public function buildSitemapIndex()
- {
- $str="<?xml version="1.0" encoding="UTF-8"?>
- ";
- $str .="<sitemapindex xmlns="http://www.google.com/schemas/sitemap/0.9">
- ";
- for ($i=0;$i<count($this->items);$i++)
- {
- $str .= "<sitemap>
- ";
- $str .= "<loc>{$this->items[$i]['loc']}</loc>
- ";
- $str .= "<lastmod>{$this->items[$i]['lastmod']}</lastmod>
- ";
- $str .="</sitemap>
- ";
- }
- $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上传至sourceincludecron目录
2、进入后台->工具->计划任务->新增
更新周期可自定,建议不要太频繁。
文章来源 CODETC,欢迎分享,转载请注明地址:
http://www.codetc.com/article-237-1.html |
|