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


上一遍《網站地圖sitemap.xml自動更新lastmod文件(PHP代碼)》代碼在實際應用中並不理想,在瀏覽器中訪問后,過一會就出現500的錯誤,不能實現夜間或者凌晨變更日期的目的。

對代碼進行了更新,把更新后的php頁面在index.php中被調用,也就是只要有人訪問網站首頁或者蜘蛛抓取時,sitemap.xml中的日期就會被變更。

test.php頁面,放在/runc/目錄下

<?php

sitemaptest();

function sitemaptest()
{
    $xml="../sitemap.xml";

    $a=filemtime($xml);        //文件修改日期

    $filetime = date("Y-n-d",$a);    //文件修改日期進行格式化

    if ( $filetime != date("Y-n-d") )        //判斷文件修改日期是否等於今天。如果等於今天日期,則什么也不做;否則修改sitemap.xml中的lastmod時間
    {
        $lastmod = '<lastmod>';
        $lastmod .= date("Y-n-d",strtotime("-1 day"));        //前一天日期date("Y-n-d",strtotime("-1 day"));substr(md5(time()), 0, 8);
        $lastmod .= "</lastmod>\n";

        $fpxml=fopen($xml,"rw+");

        while(!feof( $fpxml ))
        {
            $m = fgets( $fpxml,1024 ); //fgets逐行讀取,4096最大長度,默認為1024

            if( substr_count( $m,"<priority>0.8</priority>" ) > 0 || substr_count( $m,"<priority>1.0</priority>" ) > 0 ) //查找字符串
            {
                fwrite($fpxml, $lastmod);
            }
        }

        fclose($fpxml);
    }
}

?>

在index.php中調用test.php

<?php 

$url="test.php網址";

file_get_contents($url);

?>

 


免責聲明!

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



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