Regular visitors to fidgetwith.com may have noticed a strange anomaly to the “Most read this week” box on the right hand side.

For the last few weeks, it had shown the post “Capello names 2010 England World Cup squad” as the second most popular post.

This was rather strange. After all, the World cup was over and the post itself was over 6 weeks old – surely it could nto be as popular as it was appearing to be. There must have been another reason.

After some digging, I figured it out – Mozilla Firefox prefetching.

Prefetching is supposed to make browsing faster for Firefox users as well as checking links for malware etc. It works by looking for links to the next page in the headers of the web page. In this case, it was the really popular “Who is this myster man” post causing the “noise” for the next post along. Prefetching is not proving too popular with webmaster because of the potential vast increase in server load it causes.

The wordpress plugin I use for the “Most read this week” is the Popular this week add-on. After looking at the script it runs with, it does not check for prefetching.

Here is the section in popular-this-week.php as installed:

function ptw_countview()
{
    global $wpdb, $post, $table_prefix;

    if (!isset($post))
    {
        return;
    }

    $query = "INSERT INTO `{$table_prefix}ptw` (post_ID,hitdt) VALUES('$post->ID', NOW())";
    $wpdb->query($query);

}

After doing some digging, I discovered that Firefox uses a special header when prefetching:

X-moz: prefetch

So I could adapt the script to filter for this header. Here is the section of the script now patched:

function ptw_countview()
{
    global $wpdb, $post, $table_prefix;

    if (!isset($post))
    {
        return;
    }
if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch')
	{
		return;
	}
else
{
    $query = "INSERT INTO `{$table_prefix}ptw` (post_ID,hitdt) VALUES('$post->ID', NOW())";
    $wpdb->query($query);
}
}

This now stops the script from counting any visits if it detects the visit is from a Firefox browser that is prefetching.

I hope this helps anyone with a similar problem.

VN:F [1.9.22_1171]
Rate this:
Rating: 5.0/5 (1 vote cast)
Firefox prefetching caused problem with wordpress plugin, 5.0 out of 5 based on 1 rating