罗田县升平网络工作室,一家专业从事网站建设的工作室

资讯论坛

 找回密码
 加入论坛

快捷登录

回帖中禁止出现的内容,违者将被直接永久禁止访问,删除ID处理 :1.违反法律法规 ,包括但不限于出现带有政治、色情、暴恐信息等内容;2.恶意攻击内容,包括但不限于:恶意攻击党和政府、辱骂跟帖者、攻击主题发布者、不服从论坛管理、挑衅管理者、挑战版规等;3.广告、推广内容,尤其出现带有病毒、恶意代码、广告链接等内容,包括但不限于:QQ号、文字QQ号、微信号、手机号、文字手机号、第三方网址、单位公司名称、网站名称等;4.回帖贴出该主题隐藏资源链接或其它主题隐藏资源链接的行为。
查看: 868|回复: 0

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

[复制链接]

694

主题

735

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
19765
发表于 2019-8-31 08:58:55 | 显示全部楼层 |阅读模式
  Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页。最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度为何等),以便搜索引擎可以更加智能地抓取网站。
  对于网上很多的 Discuz sitemap插件,个人感觉大部分都很不稳定,总会出现这样那样的问题;由于Discuz 具有定时任务的功能,所以我们要自己实现自动生成sitemap.xml的话也是相应方便的。
  由于本站主要是使用Discuz的门户功能,大家可以根据以下思路自由更改,增加论坛或其它部分的sitemap,如果网站内容过多,也可以对sitemap进行拆分;具体实现这里就不详细说了,其实都比较简单
  
  1. <?php
  2.   if(!defined('IN_DISCUZ')) {
  3.   exit('Access Denied');
  4.   }
  5.   global $sm_step,$portal_url,$portal_page;
  6.   $sm_step = 2000;
  7.   $portal_page  = "article";
  8.   $sitemap_path = "/";
  9.   if(!is_dir(DISCUZ_ROOT.$sitemap_path))
  10.   {
  11.   echo "{$sitemap_path}不存在,请手工创建目录,并赋予写入权限...";
  12.   exit;
  13.   }
  14.   $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']);
  15.   create_portal_sitemap();
  16.   function create_portal_sitemap()
  17.   {
  18.   global $sm_step,$portal_url,$portal_page;
  19.   $query= DB::query ("SELECT aid, dateline FROM ".DB::table('portal_article_title')." where status = 0 ORDER BY aid DESC LIMIT 0,".$sm_step);
  20.   $portal_sitemap=new sitemap();
  21.   $line=0;
  22.   while ($row = DB::fetch($query))
  23.   {
  24.   $postdate=Date('c',$row['dateline']);
  25.   //该处地址就自行更改
  26.   $loc="http://".$portal_url."/".$portal_page."-".$row['aid']."-1.html";
  27.   $portal_sitemap->AddItem($loc,"daily","0.8",$postdate);
  28.   $line++;
  29.   }
  30.   $portal_sitemap->buildSitemap();
  31.   $portal_sitemap->SaveToFile(DISCUZ_ROOT.$sitemap_path."sitemap.xml");
  32.   unset($portal_sitemap);        
  33.   }
  34.   /*
  35.   *类名:sitemap
  36.   *说明:googlemap
  37.   *要求PHP>=5.0
  38.   *使用方法:
  39.   *
  40.   */
  41.   class sitemap{
  42.   private $items = array();
  43.   private $content="";
  44.   /**
  45.   * 增加节点
  46.   * @param string $loc  页面永久链接地址
  47.   * @param date $lastmod  页面最后修改时间,ISO 8601中指定的时间格式进行描述
  48.   * @param string $changefreq  页面内容更新频率。这里可以用来描述的单词共这几个:"always", "hourly", "daily", "weekly", "monthly", "yearly"
  49.   * @param string $priority 是用来指定此链接相对于其他链接的优先权比值。此值定于0.0 - 1.0之间
  50.   * @return  array $items
  51.   * 用法举例:)
  52.   */
  53.   public function AddItem($loc,$changefreq,$priority,$lastmod)
  54.   {
  55.   $loc=$this->replacestr($loc);
  56.   $this->items[]= array('loc'=>$loc,
  57.   'changefreq'=>$changefreq,
  58.   'priority'=>$priority,
  59.   'lastmod'=>$lastmod );
  60.   }
  61.   //替换loc特殊字符
  62.   public function replacestr($str)
  63.   {
  64.   str_replace("&","&",$str);
  65.   str_replace("'","'",$str);
  66.   str_replace(""",""",$str);
  67.   str_replace(">",">",$str);
  68.   str_replace("<","<",$str);
  69.   return $str;
  70.   }
  71.   //打印显示
  72.   public function Show()
  73.   {
  74.   if (empty($this->content))
  75.   $this->buildSitemap();
  76.   echo($this->content);
  77.   }
  78.   /**
  79.   * 生成sitemap
  80.   */
  81.   public function buildSitemap()
  82.   {
  83.   $str="<?xml version="1.0" encoding="UTF-8"?>
  84.   ";
  85.   //$str .="<urlset xmlns="http://www.google.com/schemas/sitemap/0.9">
  86.   ";
  87.   $str.="<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ";
  88.   $str.="xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ";
  89.   $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]">";
  90.   for ($i=0;$i<count($this->items);$i++)
  91.   {
  92.   $str .= "<url>
  93.   ";
  94.   $str .= "<loc>{$this->items[$i]['loc']}</loc>
  95.   ";
  96.   $str .= "<lastmod>{$this->items[$i]['lastmod']}</lastmod>
  97.   ";
  98.   $str .= "<changefreq>{$this->items[$i]['changefreq']}</changefreq>
  99.   ";
  100.   $str .= "<priority>{$this->items[$i]['priority']}</priority>
  101.   ";
  102.   $str .="</url>
  103.   ";
  104.   }
  105.   $str .= "</urlset>";
  106.   $this->content = $str ;
  107.   }
  108.   /**
  109.   * 生成sitemap
  110.   */
  111.   public function buildSitemapIndex()
  112.   {
  113.   $str="<?xml version="1.0" encoding="UTF-8"?>
  114.   ";
  115.   $str .="<sitemapindex xmlns="http://www.google.com/schemas/sitemap/0.9">
  116.   ";
  117.   for ($i=0;$i<count($this->items);$i++)
  118.   {
  119.   $str .= "<sitemap>
  120.   ";
  121.   $str .= "<loc>{$this->items[$i]['loc']}</loc>
  122.   ";
  123.   $str .= "<lastmod>{$this->items[$i]['lastmod']}</lastmod>
  124.   ";
  125.   $str .="</sitemap>
  126.   ";
  127.   }
  128.   $str .= "</sitemapindex >";
  129.   $this->content = $str ;
  130.   }
  131.   //保存文件
  132.   public function SaveToFile($filename){
  133.   $handle = fopen($filename, 'wb');
  134.   if ($handle)
  135.   {
  136.   fwrite($handle, $this->content);
  137.   fclose($handle);
  138.   }
  139.   else
  140.   {
  141.   echo ("创建失败");
  142.   }
  143.   }
  144.   //构造函数
  145.   function __construct(){
  146.   }
  147.   //析构函数
  148.   function __destruct(){
  149.   unset($this->items,$this->content);
  150.   }
  151.   }
  152.   ?>
复制代码

  使用方法 :
  1、把以上代码保存为cron_sitemap.php上传至sourceincludecron目录
  2、进入后台->工具->计划任务->新增
  
  更新周期可自定,建议不要太频繁。
  文章来源 CODETC,欢迎分享,转载请注明地址:
  http://www.codetc.com/article-237-1.html
打赏鼓励一下!
回复

使用道具 举报

回帖中禁止出现的内容,违者将被直接永久禁止访问,删除ID处理 :1.违反法律法规 ,包括但不限于出现带有政治、色情、暴恐信息等内容;2.恶意攻击内容,包括但不限于:恶意攻击党和政府、辱骂跟帖者、攻击主题发布者、不服从论坛管理、挑衅管理者、挑战版规等;3.广告、推广内容,尤其出现带有病毒、恶意代码、广告链接等内容,包括但不限于:QQ号、文字QQ号、微信号、手机号、文字手机号、第三方网址、单位公司名称、网站名称等;4.回帖贴出该主题隐藏资源链接或其它主题隐藏资源链接的行为。

浏览排行

(38463)2019-11-5 公共云钱包资金盘骗局揭秘: 网络传销+原始股骗局合体!

(22233)2019-12-20 12月17日 邓智天法院直播庭审疑问全解答!

(20722)2019-12-1 环保币GEC资金盘骗局最新消息: 即将崩盘!

(17244)2019-11-9 巨胸肥臀大长腿,嫩模糯美子真人COS不知火舞福利污图

(15868)2018-12-24 罗田县人民法院公布【第五批失信被执行人名单】 ...

(14972)2019-11-3 曝光!PTFX已经崩盘跑路,投资者血流成河!

(13018)2019-8-7 湖北电力网上缴费,支付宝绑定户号的初始密码是什么?

(12480)2018-10-17 罗田县人民政府“12345”市民服务热线服务指南

(11170)2019-12-11 公安定性了, 趣码是非法传销! 趣码怎么退回365元?

(11081)2019-12-15 满足你对女同事的幻想 风骚秘书阿朱销魂眼神勾魂摄魄

最新发表

[升平网络工作室]2025-8-23 [2025-08-23]罗田天气预报

[升平网络工作室]2025-8-23 西藏自治区成立60周年庆祝大会隆重举行 习近平出席大会

[升平网络工作室]2025-8-23 县委委员会召开查摆问题整改整治情况汇报会

[爱查小程序]2025-8-22 [爱查]在线听音乐操作说明

[升平网络工作室]2025-8-22 [2025-08-22]罗田天气预报

[升平网络工作室]2025-8-22 习近平率中央代表团抵达拉萨出席西藏自治区成立60周年庆祝活动

[升平网络工作室]2025-8-22 县关工委联合经济开发区开展“情系学子”助学活动 助力职工子女圆梦大学

[升平网络工作室]2025-8-21 2025年罗田县卫健系统赴高校公开招聘事业单位工作人员拟聘用人员公示公告

[升平网络工作室]2025-8-21 [2025-08-21]罗田天气预报

[升平网络工作室]2025-8-21 县安防委2025年度第三次全体(扩大)会召开

QQ|Archiver|手机版|小黑屋|资讯论坛BBS.SPW8.CN ( 鄂ICP备2021011341号-3 )|网站地图


手机扫一扫继续访问
[免责声明]
本站系本网编辑转载,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。
如涉及作品内容、版权和其它问题,请在30日内与本网联系,我们将在第一时间删除内容!
[声明]本站文章版权归原作者所有 内容为作者个人观点 本站只提供参考并不构成任何投资及应用建议。

进入社区 | 发表新帖 | 百度收录 |
技术提供:罗田县升平网络工作室
站长Email:admin@spw8.cn
投诉电话(刮开查看):15374567400

GMT+8, 2025-8-23 16:56 , Processed in 0.242776 second(s), 29 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表