wordpress send_headers函数造成页面等待响应时间长

分享到: 更多

找了一天终于找到出问题的地方了,注释掉这个函数后暂时正常了,页面似乎也没啥影响。

先记录下,明天继续找函数中的原因。

函数在wp-includes/class-wp.php文件中

/**
* Send additional HTTP headers for caching, content type, etc.
*
* Sets the X-Pingback header, 404 status (if 404), Content-type. If showing
* a feed, it will also send last-modified, etag, and 304 status if needed.
*
* @since 2.0.0
*/
function send_headers() {
$headers = array(‘X-Pingback’ => get_bloginfo(‘pingback_url’));
$status = null;
$exit_required = false;

if ( is_user_logged_in() )
$headers = array_merge($headers, wp_get_nocache_headers());
if ( !empty($this->query_vars['error']) && ’404′ == $this->query_vars['error'] ) {
$status = 404;
if ( !is_user_logged_in() )
$headers = array_merge($headers, wp_get_nocache_headers());
$headers['Content-Type'] = get_option(‘html_type’) . ‘; charset=’ . get_option(‘blog_charset’);
} else if ( empty($this->query_vars['feed']) ) {
$headers['Content-Type'] = get_option(‘html_type’) . ‘; charset=’ . get_option(‘blog_charset’);
} else {
// We’re showing a feed, so WP is indeed the only thing that last changed
if ( !empty($this->query_vars['withcomments'])
|| ( empty($this->query_vars['withoutcomments'])
&& ( !empty($this->query_vars['p'])
|| !empty($this->query_vars['name'])
|| !empty($this->query_vars['page_id'])
|| !empty($this->query_vars['pagename'])
|| !empty($this->query_vars['attachment'])
|| !empty($this->query_vars['attachment_id'])
)
)
)
$wp_last_modified = mysql2date(‘D, d M Y H:i:s’, get_lastcommentmodified(‘GMT’), 0).’ GMT’;
else
$wp_last_modified = mysql2date(‘D, d M Y H:i:s’, get_lastpostmodified(‘GMT’), 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);

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;
}
}

$headers = apply_filters(‘wp_headers’, $headers, $this);

if ( ! empty( $status ) )
status_header( $status );
foreach( (array) $headers as $name => $field_value )
@header(“{$name}: {$field_value}”);

if ( $exit_required )
exit();

do_action_ref_array(‘send_headers’, array(&$this));
}

加注释的地方:

    /**
* Sets up all of the variables required by the WordPress environment.
*
* The action ‘wp’ has one parameter that references the WP object. It
* allows for accessing the properties and methods to further manipulate the
* object.
*
* @since 2.0.0
*
* @param string|array $query_args Passed to {@link parse_request()}
*/
function main($query_args = ”) {
$this->init();
$this->parse_request($query_args);
//$this->send_headers();
$this->query_posts();
$this->handle_404();
$this->register_globals();
do_action_ref_array(‘wp’, array(&$this));
}

终于找到原因了,我装了一个Daily Stat的统计插件,今早发现自从注释了上面的函数这个统计插件就没有新数据了,我停用了这个插件然后恢复注释掉的函数,这下正常了。

我还是用百度统计吧,放在页面最后也不会影响速度,而且百度统计本身速度就很快。

Leave a Reply

Google AdWords