zoukankan      html  css  js  c++  java
  • 网站地图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>
    ";
    
            $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);
    
    ?>
  • 相关阅读:
    pku2351 Colored Sticks
    JSOI2010 满汉全席
    享元模式
    适配器模式
    合成模式
    原型模式
    创建型设计模式
    建造者模式
    装饰模式
    单例模式
  • 原文地址:https://www.cnblogs.com/zhuque/p/12814114.html
Copyright © 2011-2022 走看看