Home > Backend Development > PHP Tutorial > PHP operates Last-Modified and etag to achieve further pseudo-static and 304_PHP tutorial

PHP operates Last-Modified and etag to achieve further pseudo-static and 304_PHP tutorial

WBOY
Release: 2016-07-13 17:48:44
Original
1001 people have browsed it

 做了比较久的伪静态方面的探索,最后发现,用以下方法可以实现针对SEO的伪静态,同时也有减少服务器压力的效果.
>> 访问PHP页面

>> 根据内容更新时间生成Last-Modified,根据文件名与内容更新时间生成etag

>> 对客户端的数据做对比,如查看客户端浏览器的Last-Modified,对比etag等,如果相等,输出304,停止输出最新数据

>> 如果不相等,照常输出最新数据,并返回Last-Modified和etag给客户端

 /**
 * 输出head  Last-Modified 并判断是否输出304
 */
protected function _echo_last_etag($tmp_time){
    $now_url =get_absolute_url();
    $md5 = md5($now_url.$tmp_time);
    $etag = '"' . $md5 . '"';
    header('Last-Modified: '.gmdate('D, d M Y H:i:s',$tmp_time ).' GMT');
    header("ETag: $etag");
    if((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $tmp_time) || (isset($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_UNMODIFIED_SINCE']) < $tmp_time) || (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag)){
        header("HTTP/1.1 304 Not Modified"); 
        exit(0);
    }  

}


摘自 小强的专栏

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478406.htmlTechArticle做了比较久的伪静态方面的探索,最后发现,用以下方法可以实现针对SEO的伪静态,同时也有减少服务器压力的效果. 访问PHP页面 根据内容更...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template