Hongmu Notes
Home Program Notes Empire uses PHP to generate sitemap files
Program Notes Empire cms

Empire uses PHP to generate sitemap files

Empire uses PHP to generate sitemap files

This code can be used directly, but there is one thing to note. If the website has too many articles, you need to modify some of the code. Baidu seems to stipulate the number of links in the sitemap. Too many and not very friendly

sitemap code

<?php  
/*引用文件*/  
require('e/class/connect.php');
require('e/class/db_sql.php');
require('e/data/dbcache/class.php');
/*建立数据库链接 与 实例化类*/  
$link = db_connect();  
$empire=new mysqlquery();  
//header("Content-type:application/xml");
header("Content-type: text/html; charset=utf-8");
$operation = $_GET['operation'];
$current_url = explode('/',$_SERVER['PHP_SELF']);  
$current_script_name = end($current_url);  
if($operation && $operation == "makexml")
{
 $xml = generate_sitemp_xml();
 $result = save_xml('sitemap.xml',$xml);
 if($result)
 {
  echo '生成成功,<a href="sitemap.xml">点此查看</a>'; 
 }
 else
 {
  echo '生成失败,<a href="'.$current_script_name.'">在来一次</a>!'; 
 }
}
else
{
 echo '欢迎使用红穆Sitemap生成工具,请<a href="'.$current_script_name.'?operation=makexml">点此生成</a>!'; 
}

//保存到文件
function save_xml($filename, $text) {
 if (!$filename || !$text)
  return false;
 
   @chmod($filename,0777);
 if ($fp = fopen($filename, "w")) {
  if (@fwrite($fp, $text)) {
   fclose($fp);
   return true;
  } else {
   fclose($fp);
   return false;
  } 
 } 
    return false;
} 
//生成Sitemap XML数据
function generate_sitemp_xml()
{
 global $dbtbpre,$empire;
 
 $xml = '';
 $xml .= '<?xml version="1.0" encoding="utf-8"?>' .PHP_EOL;
 $xml .= '<urlset>' .PHP_EOL;
 $xml .= generate_home_xml();//首页
 $xml .= generate_class_xml();//栏目
 
 $query="select * from {$dbtbpre}ecms_news order by id desc limit 10000";
 $sql=$empire->query($query);
 while($r=$empire->fetch($sql))
 {
  $titleurl=sys_ReturnBqTitleLink($r);
  
  $xml .= '  <url>' .PHP_EOL;
  $xml .= '    <loc>'.$titleurl.'</loc>' .PHP_EOL;
  $xml .= '    <lastmod>'.strftime('%Y-%m-%d',$r['newstime']).'</lastmod>' .PHP_EOL;
  $xml .= '    <changefreq>daily</changefreq>' .PHP_EOL;
  $xml .= '    <priority>0.8</priority>' .PHP_EOL;
  $xml .= '  </url>' .PHP_EOL;
  
 }
 $xml .= '</urlset>' .PHP_EOL; 
 
 return $xml;
}
//生成栏目链接XML数据
function generate_class_xml()
{
 global $dbtbpre,$empire,$class_r;
 $xml = '';
  
 $sql=$empire->query("SELECT * FROM {$dbtbpre}enewsclass WHERE islast=1");
 while($r=$empire->fetch($sql))
 {
  $infor=$empire->fetch1("SELECT newstime FROM {$dbtbpre}ecms_".$class_r[$r[classid]][tbname]." WHERE classid='$r[classid]' ORDER BY newstime DESC LIMIT 1");
  $class_url=sys_ReturnBqClassname($r, 9);
  
  $xml .= '  <url>' .PHP_EOL;
  $xml .= '    <loc>'.$class_url.'</loc>' .PHP_EOL;
  $xml .= '    <lastmod>'.strftime('%Y-%m-%d',time()).'</lastmod>' .PHP_EOL;
  $xml .= '    <changefreq>daily</changefreq>' .PHP_EOL;
  $xml .= '    <priority>0.8</priority>' .PHP_EOL;
  $xml .= '  </url>' .PHP_EOL;
  
 }
 
 return $xml;
}
//生成栏目链接XML数据
function generate_home_xml()
{
global $public_r; 
 $xml = '';
 $xml .= '  <url>' .PHP_EOL;
 $xml .= '    <loc>'.$public_r[newsurl].'</loc>' .PHP_EOL;
 $xml .= '    <lastmod>'.strftime('%Y-%m-%d',time()).'</lastmod>' .PHP_EOL;
 $xml .= '    <changefreq>daily</changefreq>' .PHP_EOL;
 $xml .= '    <priority>0.8</priority>' .PHP_EOL;
 $xml .= '  </url>' .PHP_EOL;
 
 return $xml;
}
/*关闭数据库连接 与 释放类*/  
db_close();  
$empire=null;
?>
微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

Empire CMS database statement & SQL statement format

Empire CMS database statement & SQL statement format Program Notes Empire cms

Introduction to Imperial CMS extended SQL program writing, basic examples of Imperial CMS database statements & SQL statement formats: Note: The following examples are based on placing PHP files in the system root directory. Example 1: Connect to MYSQL program. (a.php)<?php require('e/class/connect.php'); //Introduce database configuration files and public function files requ…
👁 351
Empire CMS obtains the system COOKIE variable function getcvar()

Empire CMS obtains the system COOKIE variable function getcvar() Program Notes Empire cms

Get the system COOKIE variable function syntax: getcvar($var,$ecms) Description: $var: is the variable name $ecms: 0 is to set the foreground COOKIE variable, 1 is to set the background COOKIE variable. This parameter can be omitted and defaults to 0. Usage example: getcvar('mlusername'), get the user name of the front-end login member getcvar('loginu...
👁 295
Empire CMS smart label call field collection

Empire CMS smart label call field collection Program Notes Empire cms

Collect and classify all fields that support smart tag calls into Empire CMS smart tags: [e:loop={column ID/topic ID, number of items displayed, operation type, only display pictures with titles, additional SQL conditions, display sorting}] Template code content [/e:loop] Calling time: <?=date('m-d',$bqr[newstime])?> <?=dat…
👁 3749

Recommended reading

Higher Education Institution Website Template (College/University Category) 0831

Higher Education Institution Website Template (College/University Category) 0831 Practical Collection Yiyou template

An EyouCMS website template designed specifically for universities and colleges. Its professional and dignified design is ideal for showcasing institutional profiles, departmental structures, admissions information, and campus culture. This template helps universities attract students online and enhance their brand image. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Ey...
👁 46
Responsive school/college website template – 0832

Responsive school/college website template – 0832 Practical Collection Yiyou template

This EYO CMS responsive template is ideal for schools, universities, and academic institutions. Its educational-themed design is perfect for showcasing school environments, teaching achievements, admissions information, and campus news, helping educational organizations attract students and parents online. Template Demo | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 56
Responsive Science and Engineering Experimental Foreign Language School College Website Template 0833

Responsive Science and Engineering Experimental Foreign Language School College Website Template 0833 Practical Collection Yiyou template

An eYouCMS responsive website template designed for science and engineering institutions, laboratories, foreign language schools, and academic departments. Its technology-driven design is ideal for showcasing laboratory facilities, foreign language teaching programs, research achievements, and admissions information, helping science and foreign language-focused institutions effectively highlight their unique strengths online. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eYouCMS | eYouCMS...
👁 53
Resource website typecho001 template

Resource website typecho001 template Program Notes Typecho

typecho001 template template please do not modify the folder name of this template. The folder name is: typecho001 1.4 Fix the js output problem on the article page 1.3 Fix the error prompt when the plug-in is not installed Optimize the list page code output 1.2 Fix the comment function and add a custom homepage title 1.1 Fix the comment reply asymmetry function Add a separate title setting function on the homepage Add the website favicon.ico icon Add one...
👁 198
Vocational Education and Technology College School Website Template 0834

Vocational Education and Technology College School Website Template 0834 Practical Collection Yiyou template

This EyouCMS template is ideal for vocational and technical colleges, featuring a professional design style perfect for showcasing vocational education curricula, practical training facilities, employment services, and admissions information. It helps vocational institutions attract students online and enhance their brand awareness. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 52
Modification of typecho paging style

Modification of typecho paging style Program Notes Typecho

Sir, times have changed! Typecho is currently the most perfect solution, because Baidu can only see the code of fixed thinking. The actual generated HTML code is breathtakingly clean and fully customized, including adding classes to the li element, adding classes to the a element, adding classes to the previous page and next page, and removing the li tags that come with typecho to express more. I can even add some text to the content inside...
👁 517