Prevent Facebook Social Plugin to load page twice – CodeIgniter

facebook_logo

Recently, I discovered one of my web project build on top of CodeIgniter is having issues with duplicate page views. It only happens to FireFox and Google Chrome browser. It looks just find on IE, Opera and Safari browser. So I keep researching and researching and finally found out that Facebook Social Plugin is hitting the page after the initial page load from the browser triggered by the end user.

I am not sure why it doesn’t happen to browsers other than FireFox and Google Chrome. There has to be a way to prevent this from screwing up my page view counter.

The project is currently using CodeIgniter as PHP framework. So I have a SQL query to add one page view when user view the page. Now the query was execute twice every time the page is loaded. After removing components on the page, I found out Facebook LIKE social plugin is the one that causes the duplicate page load. I guess Facebook is constantly loading the page and see if the social graph information is updated.

In the Apache access log, I found there is a non-browser and non-robot user agent that hit the page. It looks like this.

facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)

If you look at the Facebook’s externalhit user agent text page, it says this.

Why does Facebook appear in my server logs?

Facebook allows its users to send links to interesting web content to other Facebook users. Part of how this works on the Facebook system involves the temporary display of certain images or details related to the web content, such as the title of the webpage or the embed tag of a video. Our system retrieves this information only after a user provides us with a link. You may have found this page because a Facebook user sent a link from your website to other Facebook users. If you have any questions or concerns about any links or content sent by one of our users, please contact us at legal@facebook.com.

The only way to distinguish this agent is to use user_agent class and $this->agent->is_browser() function. In the case, I won’t update the page view counter to and also to prevent other robots from generating duplicate page view.

Problem solved!

Related Posts Plugin for WordPress, Blogger...

Share your thoughts with me!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge
Loading Facebook Comments Widget ...