wordpress未登录用户304

这是从鱼叔那里拿来的,如果只是普通的个人博客的话,可以减轻服务器的压力,如果要做wommece的话还是别用了,可能存在一点问题。


//未登陆304
function wpjam_headers($headers,$wp){
    if(!is_user_logged_in() && empty($wp->query_vars['feed'])){
        $headers['Cache-Control']   = 'max-age:600';
        $headers['Expires']         = gmdate('D, d M Y H:i:s', time()+600) . " GMT";
        $wpjam_timestamp = get_lastpostmodified('GMT')>get_lastcommentmodified('GMT')?get_lastpostmodified('GMT'):get_lastcommentmodified('GMT');
        $wp_last_modified = mysql2date('D, d M Y H:i:s', $wpjam_timestamp, 0).' GMT';
        $wp_etag = '"' . md5($wp_last_modified) . '"';
        $headers['Last-Modified'] = $wp_last_modified;
        $headers['ETag'] = $wp_etag;
        // Support for Conditional GET
        if (isset($_SERVER['HTTP_IF_NONE_MATCH']))
            $client_etag = stripslashes(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']));
        else $client_etag = false;
        $client_last_modified = empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? '' : trim($_SERVER['HTTP_IF_MODIFIED_SINCE']);
        // If string is empty, return 0. If not, attempt to parse into a timestamp
        $client_modified_timestamp = $client_last_modified ? strtotime($client_last_modified) : 0;
        // Make a timestamp for our most recent modification...
        $wp_modified_timestamp = strtotime($wp_last_modified);
        $exit_required = false;
        if ( ($client_last_modified && $client_etag) ?
                 (($client_modified_timestamp >= $wp_modified_timestamp) && ($client_etag == $wp_etag)) :
                 (($client_modified_timestamp >= $wp_modified_timestamp) || ($client_etag == $wp_etag)) ) {
            $status = 304;
            $exit_required = true;
        }
        if ( $exit_required ){
            if ( ! empty( $status ) ){
                status_header( $status );
            }
            foreach( (array) $headers as $name => $field_value ){
                @header("{$name}: {$field_value}");
            }

            if ( isset( $headers['Last-Modified'] ) && empty( $headers['Last-Modified'] ) && function_exists( 'header_remove' ) ){
                @header_remove( 'Last-Modified' );
            }  
            exit();    
        } 
    } 
    return $headers;
}
Last modification:October 23rd, 2017 at 01:04 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment