網站地圖sitemap.xml自動更新lastmod文件(PHP代碼)


網站地圖sitemap.xml自動更新lastmod文件(PHP代碼)

目前我這個代碼是用到我網站上的,每個一定時間去執行一段代碼,更新我的xml地圖,可以借助這個思路,實現其他軟件測試輔助功能。比如定期獲取服務器上的某類資源。

 1 <?php
 2 ignore_user_abort();    //關閉瀏覽器仍然執行
 3 
 4 set_time_limit(0);    //讓程序一直執行下去
 5 
 6 ini_set('memory_limit','52M');        //設置內存限制
 7 
 8 $interval=60*60;    // 每隔1小時運行一次
 9 
10 do{
11     sitemaptest();
12     
13     sleep($interval);
14     
15 }while(true);
16 
17 
18 function sitemaptest()
19 {
20     $xml="../sitemap.xml";
21 
22     $a=filemtime($xml);
23 
24     $filetime = date("Y-n-d",$a);
25 
26     //if ( $filetime == date("Y-n-d")  )    
27     //{
28     //    null;
29     //}else 
30     if ( $filetime != date("Y-n-d") )        //判斷文件修改日期是否等於今天。如果等於今天日期,則什么也不做;否則修改sitemap.xml中的lastmod時間
31     {
32         $lastmod = '<lastmod>';
33         $lastmod .= date("Y-n-d",strtotime("-1 day"));        //前一天日期
34         $lastmod .= "</lastmod>\n";
35 
36         $fpxml=fopen($xml,"rw+");
37 
38         while(!feof( $fpxml ))
39         {
40             $m = fgets( $fpxml,1024 ); //fgets逐行讀取,4096最大長度,默認為1024
41 
42             if( substr_count( $m,"<priority>0.8</priority>" ) > 0 || substr_count( $m,"<priority>1.0</priority>" ) > 0 ) //查找字符串
43             {
44                 fwrite($fpxml, $lastmod);
45             }
46         }
47 
48         fclose($fpxml);
49     }
50 }
51 
52 ?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM