php语言

PHP生成SiteMap文件代码的方法

时间:2025-03-01 02:50:55 php语言 我要投稿
  • 相关推荐

PHP生成SiteMap文件代码的方法

  分享一例php生成sitemap地图文件的代码,用于生成google sitemap地图,有需要的朋友参考学习下。

  例子:

  复制代码 代码示例:

  <?php

  /**

  * 生成sitemap文件

  * 平常在用的一段代码,分享给大家

  * edit: www.jbxue.com

  */

  require_once('conn.php');

  $doc = new DOMDocument('1.0', 'utf-8');  // 声明版本和编码

  $doc -> formatOutput = true;  //格式XML输出

  $sql="select mid from dede_member_company";

  $query=mysql_query($sql);

  $count=mysql_num_rows($query);

  $pg=intval($count/3000);

  if($count%3000)

  $pg++;

  $page=isset($_GET['page'])?$_GET['page']:1;

  $getpageinfo=page($page,$count,3000);

  $sql="SELECT `mid`,`uptime`,`html_path` FROM `dede_member_company` order by `mid`  $getpageinfo[sqllimit]";

  $query=mysql_query($sql);

  $urlset    = $doc -> createElement('urlset');

  $xmlns    = $doc -> createAttribute('xmlns');

  $xmlnsvalue  = $doc -> createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");

  $xmlnsxsi    = $doc -> createAttribute('xmlns:xsi');

  $xmlnsxsivalue  = $doc -> createTextNode("http://www.w3.org/2001/XMLSchema-instance");

  $schemaLocation    = $doc -> createAttribute('xsi:schemaLocation');

  $schemaLocationvalue  = $doc -> createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");

  $xmlns -> appendChild($xmlnsvalue);

  $xmlnsxsi -> appendChild($xmlnsxsivalue);

  $schemaLocation -> appendChild($schemaLocationvalue);

  $urlset       -> appendChild($xmlns);

  $urlset       -> appendChild($schemaLocation);

  $urlset       -> appendChild($xmlnsxsi);

  while($row=mysql_fetch_array($query)){

  $url    = $doc -> createElement('url');    //创建一个标签

  $loc    = $doc -> createElement('loc');    //创建一个标签

  $id    = $doc -> createAttribute('id');      //创建一个属性

  $newsid  = $doc -> createTextNode($row[mid]);        //设置属性内容

  $newsco = $doc -> createTextNode($row[html_path]);      //设置标签内容

  $lastmod    = $doc -> createElement('lastmod');

  $modtime  = $doc -> createTextNode(date('c',$row[uptime]));

  $changefreq    = $doc -> createElement('changefreq');

  $freqtype  = $doc -> createTextNode('daily');

  $priority   = $doc -> createElement('priority');

  $prival  = $doc -> createTextNode('0.5');

  $id        -> appendChild($newsid);    //继承属性

  $loc    -> appendChild($id);  //继承属性内容

  $loc    -> appendChild($newsco);    //继承标签内容      //继承子类

  $lastmod ->appendChild($modtime);

  $changefreq ->appendChild($freqtype);

  $priority ->appendChild($prival);

  $url    -> appendChild($loc);

  $url    -> appendChild($lastmod);

  $url    -> appendChild($changefreq);

  $url    -> appendChild($priority);

  $urlset    -> appendChild($url);

  }

  $doc    -> appendChild($urlset);

  $doc    -> save("../map/sitemap_".$page.".xml");

  //=================================

  if($page=="1"){

  $main_sitemap = new DOMDocument('1.0', 'utf-8');  // 声明版本和编码

  $main_sitemap -> formatOutput = true;  //格式XML输出

  $sitemapindex    = $main_sitemap -> createElement('sitemapindex');

  $xmlns    = $main_sitemap -> createAttribute('xmlns');

  $xmlnsvalue  = $main_sitemap -> createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");

  $sitemap    = $main_sitemap -> createElement('sitemap');    //创建一个标签

  $loc    = $main_sitemap -> createElement('loc');    //创建一个标签

  $id    = $main_sitemap -> createAttribute('id');      //创建一个属性

  $newsid  = $main_sitemap -> createTextNode($page);        //设置属性内容

  $newsco = $main_sitemap -> createTextNode("http://youdomain/map/sitemap_".$page.".xml");      //设置标签内容

  $xmlns -> appendChild($xmlnsvalue);

  $sitemapindex        -> appendChild($xmlns);

  $id        -> appendChild($newsid);    //继承属性

  $loc    -> appendChild($id);  //继承属性内容

  $loc    -> appendChild($newsco);    //继承标签内容

  $sitemap    -> appendChild($loc);        //继承子类

  $sitemapindex    -> appendChild($sitemap);

  $main_sitemap   -> appendChild($sitemapindex);

  $main_sitemap    -> save("../sitemap.xml");

  }else{

  $main_sitemap = new DOMDocument('1.0', 'utf-8');  // 声明版本和编码

  $main_sitemap->preserveWhiteSpace = false;

  $main_sitemap -> formatOutput = true;  //格式XML输出

  $main_sitemap->load("../sitemap.xml");

  $notes = $main_sitemap->documentElement;

  $sitemap    = $main_sitemap -> createElement('sitemap');    //创建一个标签

  $loc    = $main_sitemap -> createElement('loc');    //创建一个标签

  $id    = $main_sitemap -> createAttribute('id');      //创建一个属性

  $newsid  = $main_sitemap -> createTextNode($page);        //设置属性内容

  $newsco = $main_sitemap -> createTextNode("http://youdomain/map/sitemap_".$page.".xml");      //设置标签内容

  $id        -> appendChild($newsid);    //继承属性

  $loc    -> appendChild($id);  //继承属性内容

  $loc    -> appendChild($newsco);    //继承标签内容

  $sitemap    -> appendChild($loc);        //继承子类

  $notes    -> appendChild($sitemap);

  $main_sitemap    -> appendChild($notes);

  $main_sitemap    -> save("../sitemap.xml");

  }

  //

  if($page<$pg){

  echo "sitemap_".$page."生成完成,正进行下一个生成";

  $page++;

  echo "<script>location.href='?page=".$page."';</script>";

  }else{

  echo "生成完成!";

  exit();

  }

  function page($page,$total,$pagesize=10,$pagelen=7){

  $url=$_SERVER["REQUEST_URI"];

  $parse_url=parse_url($url);

  $url_path=$parse_url[path];

  $url_query=$parse_url[query];

  $page=$_GET[page];

  if($url_query){

  $url_query=ereg_replace("(^|&)page=$page","",$url_query);

  $url=str_replace($parse_url["query"],$url_query,$url);

  if($url_query) $url.="&page"; else $url.="page";

  }else {

  $url.="?page";

  }

  $phpfile=$url;

  $pagecode = '';//定义变量,存放分页生成的HTML

  $page = intval($page);//避免非数字页码

  $total = intval($total);//保证总记录数值类型正确

  if(!$total) return array();//总记录数为零返回空数组

  $pages = ceil($total/$pagesize);//计算总分页

  //处理页码合法性

  if($page<1) $page = 1;

  if($page>$pages) $page = $pages;

  //计算查询偏移量

  $offset = $pagesize*($page-1);

  //页码范围计算

  $init = 1;//起始页码数

  $max = $pages;//结束页码数

  $pagelen = ($pagelen%2)?$pagelen:$pagelen+1;//页码个数

  $pageoffset = ($pagelen-1)/2;//页码个数左右偏移量

  //生成html

  $pagecode='<p class="page">';

  $pagecode.="<span>$page/$pages</span>";//第几页,共几页

  //如果是第一页,则不显示第一页和上一页的连接

  if($page!=1){

  $pagecode.="<a href=\"{$phpfile}=1\">&lt;&lt;</a>";//第一页

  $pagecode.="<a href=\"{$phpfile}=".($page-1)."\">&lt;</a>";//上一页

  }

  //分页数大于页码个数时可以偏移

  if($pages>$pagelen){

  //如果当前页小于等于左偏移

  if($page<=$pageoffset){

  $init=1;

  $max = $pagelen;

  }else{//如果当前页大于左偏移

  //如果当前页码右偏移超出最大分页数

  if($page+$pageoffset>=$pages+1){

  $init = $pages-$pagelen+1;

  }else{

  //左右偏移都存在时的计算

  $init = $page-$pageoffset;

  $max = $page+$pageoffset;

  }

  }

  }

  //生成html

  for($i=$init;$i<=$max;$i++){

  if($i==$page){

  $pagecode.='<span>'.$i.'</span>';

  } else {

  $pagecode.="<a href=\"{$phpfile}={$i}\">$i</a>";

  }

  }

  if($page!=$pages){

  $pagecode.="<a href=\"{$phpfile}=".($page+1)."\">&gt;</a>";//下一页

  $pagecode.="<a href=\"{$phpfile}={$pages}\">&gt;&gt;</a>";//最后一页

  }

  $pagecode.='</p>';

  return array('pagecode'=>$pagecode,'sqllimit'=>' limit '.$offset.','.$pagesize);

  }

  ?>

【PHP生成SiteMap文件代码的方法】相关文章:

PHP生成SiteMap文件的代码09-27

php sitemap文件生成器代码07-11

php自动生成sitemap地图代码10-21

php自动生成sitemap地图的代码10-07

php自动生成sitemap地图的代码07-31

PHP中BB生成sitemap地图文件的方法11-06

php生成sitemap.xml的实例代码06-02

PHPBB生成sitemap地图文件的方法10-15

php动态生成JavaScript代码10-03