raksmart活动促销

分享

写回答

发帖

自动生成符合GOOGLE要求的网站地图文件

互联网出海创业 互联网出海创业 4824 人阅读 | 0 人回复

发表于 2006-7-25 23:30:58 | 显示全部楼层 |阅读模式

PHP 代码:


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN"><head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  4. <meta name="generator" content="HAPedit 3.1" />
  5. <style type="text/css">
  6. body {
  7.     text-align: left;
  8.     background-color: #FFFFFF;
  9. }

  10. .center {
  11.     text-align: center;
  12. }

  13. .italic  {
  14.     font-style: italic;
  15. }
  16. </style>
  17. <title>Google网站地图生成器</title>

  18. </head>
  19. <body id="top">
  20. <h2 class="center">自动生成符合GOOGLE要求的网站地图文件</h2>
  21. <p class="center">Copyright <a href="http://topflood.free.fr"  target ="_blank" title="原作者站">Top flood</a><br />
  22. 原作者 : <br />
  23. Kloh : <a href="http://timesplittersgames.free.fr"  target="_blank" title ="作者1">TimeSplitters' Games</a>; ImmortalPC :<a href="http: //immortalpc.free.fr"  target="_blank" title="作者2">Immortal-PC< /a><br /><br />
  24. <pre>
  25. 注:<br />
  26. 1.请把本程序放入网站根目录执行<br />
  27. 2.程序执行前请修改如下参数(或常量)以符合你网站要求:<br />
  28. LIMITLIENPARFICHIER
  29. LIMITLIENINDEXE
  30. $ExtensionsAutorises
  31. $DossiersInterdits
  32. $FichiersInterdits
  33. <br />
  34. 3.程序将把列表写入多个xml文件(如果你网页比较多的话)<br />
  35. 4.请在根目录下建立文件夹sitemap,并保证可写。sitemap(x).xml文件将写入这个目录<br />
  36. 5.sitemap生成之后请至<a href="" title="submit sitemap">http: //www.google.com/webmasters/sitemaps/</a> 提交给Google<br />
  37. </pre>
  38. </p>

  39. <h2>URL列表(将写入sitemap)中。。。</h2>

  40. <?php
  41. define('LIMITLIENPARFICHIER',1000); //每个文件里最大URL数
  42. define('LIMITLIENINDEXE',50000); //最大URL数(总数)

  43. $racine= 'http://'.$_SERVER['HTTP_HOST'];
  44. $compressionGZ = false;
  45. $Goption=0;
  46. $ExtensionsAutorises= array('php','php3','html','htm'); //将被写入SITEMAP的文件类型
  47. $DossiersInterdits = array('includes','modules','sessions','images','pnadodb','webanalyse','_flood','javascript','language','parameters','smileys','themes','_streifer'); //忽略的文件夹(不被列表,不被写入SITEMAP)
  48. $FichiersInterdits = array('config.php','config_i.php','streifer_forum.gif.php','xmlrpc.php','referer.php','config-old.php','test.php','admin.php','image.php','header.php','footer.php','error.php','confige.php','banners.php','pntables.php','print.php','mainfile.php','modules.php','backend.php');//忽略的文件(不被写入SITEMAP)

  49. $myfiles=GetDirContents('.');


  50. function Dossier_Autoris($DossierCourant){
  51.     global $DossiersInterdits;
  52.     return Est_Autoris($DossierCourant, $DossiersInterdits);
  53. }


  54. function Fichier_Autoris($FichierCourant){
  55.     global $FichiersInterdits;
  56.     return Est_Autoris($FichierCourant, $FichiersInterdits);
  57. }

  58. function Extension_Autoris($ExtensionCourante){
  59.     global $ExtensionsAutorises;
  60.     return !Est_Autoris($ExtensionCourante,$ExtensionsAutorises);
  61. }


  62. function Est_Autoris($DossierCourant,$Interdits){
  63.     global $Goption;

  64.     $drapeau = true;
  65.     while ($drapeau && list(,$Dossier)=each($Interdits) ){
  66.         if ( ComparaisonFichier($DossierCourant,$Dossier,$Goption))$drapeau = false;
  67.     }
  68.     reset($Interdits);
  69.     return $drapeau;
  70. }

  71. function ComparaisonFichier($DossierCourant,$Dossier,$option=0){
  72.     switch ($option){
  73.         case 0:

  74.             return ($DossierCourant == $Dossier);
  75.         break;

  76.         case 1:

  77.             $pos = strpos($mystring, $findme);
  78.             if ($pos === false) {
  79.                 return false;
  80.             } else {
  81.                 return true;
  82.             }
  83.         break;

  84.         case 2:

  85.             return ereg($Dossier,$DossierCourant);
  86.         break;
  87.     }
  88. }

  89. function getextension($fichier){
  90.     $bouts = explode('.', $fichier);
  91.     return array_pop($bouts);
  92. }


  93. function GetDirContents($dir){
  94.     global $racine;
  95.     $i=0;
  96.     ini_set('max_execution_time',10);
  97.     if (!is_dir($dir)){die ('PROBLEME: '.$dir.'!');}


  98.     if ($root=@opendir($dir)){
  99.         while ($file=readdir($root)){
  100.             if($file=='.' || $file=='..'){continue;}
  101.             if(is_dir($dir.'/'.$file) && Dossier_Autoris($file)){

  102.                 if(!IsSet($files)){$files = NULL;}
  103.                 $files=array_merge($files,GetDirContents($dir.'/'.$file));

  104.             }else{

  105.                 $extension=getextension($file);

  106.                 if (Extension_Autoris($extension) && Fichier_Autoris($file)){
  107.                     echo '<strong>',$dir,'</strong>/',htmlentities($file),'<br />',"\r\n";
  108.                     $files[$i]['lien']=utf8_encode($racine.substr($dir,1).'/'.$file);


  109.                     $modi_fich=filemtime($dir.'/'.$file);
  110.                     $files[$i]['date']=date('Y-m-d', $modi_fich);
  111.                     $i++;
  112.                 }
  113.             }
  114.         }
  115.     }
  116.     if(!IsSet($files)){$files = NULL;}
  117.     return $files;
  118. }




  119. $nbliens=count($myfiles);
  120. echo '<span class="italic">'.$nbliens.' 行网址将被写入sitemap文件.</span><br />',"\r\n";

  121. if ($nbliens>LIMITLIENPARFICHIER){
  122.     // utilisation de la norme pour les sites souhaitant rfrencs plus de 1000 liens
  123.     // un fichier sitemap à gnrer en plus
  124.     $numfichier=1;
  125.     echo '<h2>分步生成文件 sitemapXX.xml...</h2>',"\r\n";
  126. }else {
  127.     $numfichier='';
  128.     echo '<h2>sitemap.xml文件生成完毕...</h2>',"\r\n";
  129. }

  130. if ($compressionGZ)
  131. {
  132.     $open='gzopen';
  133.     $write='gzwrite';
  134.     $close='gzclose';
  135.     $GZ='.gz';
  136. } else
  137. {
  138.     $open='fopen';
  139.     $write='fwrite';
  140.     $close='fclose';
  141.     $GZ='';
  142. }

  143. $CurLiens=0;
  144. while ($CurLiens<$nbliens && $CurLiens<LIMITLIENINDEXE )
  145. {
  146.     if ($fp = $open('./sitemap/sitemap'.$numfichier.'.xml'.$GZ, 'w')){
  147.         $write($fp,'<?xml version="1.0" encoding="UTF-8"?>'."\r\n");
  148.         $write($fp,'<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">'."\r\n");
  149.         $Limite = $CurLiens + LIMITLIENPARFICHIER;

  150.         while ($CurLiens< $Limite && $CurLiens<LIMITLIENINDEXE && list(,$file)=each($myfiles))
  151.         {
  152.             $write($fp,'<url> '."\r\n".' <loc>'.$file['lien'].'</loc> '."\r\n \r\n");
  153.             $write($fp,"\t\t".'<lastmod>'.$file['date'].'</lastmod>'."\n");
  154.             $write($fp,'<changefreq>daily</changefreq>');
  155.             $write($fp,'<priority>0.8</priority></url>');
  156.             $CurLiens ++;
  157.         }
  158.         $write($fp, '</urlset>');
  159.         $close($fp);
  160.         echo '<a href="./sitemap/sitemap'.$numfichier.'.xml'.$GZ.'" target="_blank">生成文件 sitemap'.$numfichier.'.xml'.$GZ.'</a><br />',"\r\n";
  161.     }else{

  162.         echo 'sitemap'.$numfichier.'.xml',"\r\n"
  163.         ,'<br /><br /><textarea rows="30" cols="100">',"\r\n"
  164.         ,'<?xml version="1.0" encoding="UTF-8"?>',"\r\n"
  165.         ,'<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">',"\r\n";

  166.      $Limite = $CurLiens + LIMITLIENPARFICHIER;
  167.     while ($CurLiens< $Limite  && $CurLiens<LIMITLIENINDEXE && list(,$file)=each($myfiles))
  168.     {
  169.         echo '<url> '."\r\n".' <loc>'.$file['lien'].'</loc> '."\r\n ";
  170.         echo '<lastmod>'.$file['date'].'</lastmod>'."\r\n";
  171.         echo '<changefreq>monthly</changefreq>'."\r\n";
  172.         echo '<priority>0.5</priority></url>'."\r\n";
  173.         $CurLiens ++;
  174.     }
  175.     echo  '</urlset></textarea><br />';

  176. }
  177. $numfichier++;
  178. }

  179. if ($numfichier!=1)
  180. {
  181.     echo '<h2>写入 sitemap 主文件...</h2>';

  182.     if ($fp = fopen('./sitemap/sitemap.xml', 'w+')){

  183.         fwrite($fp, '<?xml version="1.0" encoding="UTF-8"?>'."\r\n");
  184.         fwrite($fp, '<sitemapindex xmlns="http://www.google.com/schemas/sitemap/0.84">'."\r\n");
  185.         $date=date('Y-m-d');

  186.         for($k=1;$k<$numfichier;$k++)
  187.         {
  188.             fwrite($fp, '<sitemap>'."\r\n");
  189.             fwrite($fp,'<loc>'.$racine.'/sitemap/sitemap'.$k.'.xml'.$GZ.'</loc>'."\r\n");
  190.             fwrite($fp, '<lastmod>'.$date.'</lastmod>'."\r\n");
  191.             fwrite($fp,'</sitemap>'."\r\n");

  192.         }
  193.         fwrite($fp, '</sitemapindex>'."\r\n");
  194.         fclose($fp);
  195.         echo '<a href="./sitemap/sitemap.xml" target="_blank">生成文件 sitemap.xml</a><br />',"\r\n";

  196.     } else
  197.     {
  198.         echo '<br /><br /><textarea rows="30" cols="100">',"\r\n"
  199.         ,'<?xml version="1.0" encoding="UTF-8"?>',"\r\n"
  200.         ,'<sitemapindex xmlns="http://www.google.com/schemas/sitemap/0.84">',"\r\n";
  201.         $date=date("Y-m-d");
  202.         for($k=1;$k<$numfichier;$k++)
  203.         {
  204.         echo '<sitemap>',"\r\n"
  205.         ,'<loc>',$racine,'/sitemap/sitemap',$k,'.xml</loc>',"\r\n"
  206.         ,'<lastmod>',$date,'</lastmod>',"\r\n"
  207.         ,'</sitemap>',"\r\n";
  208.         }
  209.         echo '</sitemapindex>',"\r\n";

  210.     }
  211. }
  212. ?>
  213. <span class="italic">全部生成完毕</span>
  214. <p class="center"><a href="#top" title="回到页面顶部">TOP</a></p>
  215. </body>

  216. </html>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则