How to recognize Facebook User-Agent

When sharing one of my pages on FB, I want to display something different. Problem is, I prefer not to use the og: elements, but to recognize FB user-agent.

What is it? I can't find it.


For list of user-agent strings, look up here. The most used, as of September 2015, are facebookexternalhit/* and Facebot. As you haven't stated what language you're trying to recognize the user-agent in, I can't tell you more information. If you do want to recognize Facebook bot in PHP, use

if (
    strpos($_SERVER["HTTP_USER_AGENT"], "facebookexternalhit/") !== false ||          
    strpos($_SERVER["HTTP_USER_AGENT"], "Facebot") !== false
) {
    // it is probably Facebook's bot
}
else {
    // that is not Facebook
}

UPDATE: Facebook has added Facebot to list of their possible user-agent strings, so I've updated my code to reflect the change. Also, code is now more predictible to possible future changes.


"Facebook's user-agent string is facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)..."

Hi

Small, yet important, correction -> Facebook external hit uses 2 different user agents:

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

Setting you fitler to 1.1 only may cause filtering issues with 1.0 version.

For more information about Facebook Bot (and other bots) please refer to Botopedia.org - a Comunity-Sourced bot directory, powered by Incapsula.

Besides user-agent data, the directory also offers an IP verification option, allowing you to cross-verify an IP/User-Agent, thus helping to prevent impersonation attempts.


Here are the Facebook crawlers User Agent:

FacebookExternalHit/1.1
FacebookExternalHit/1.0

or

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

Note that the version numbers might change. So use a regular expression to find the crawler name and then display your content.

Update:

You can use this code in PHP to check for Facebook User Agent

if(preg_match('/^FacebookExternalHit\/.*?/i',$agent)){
    print "Facebook User-Agent";
    // process here for Facebook
}

Here is ASP.NET code. You can use this function to check if the userAgent is Facebook's useragent.

public static bool IsFacebook(string userAgent)  
{  
    userAgent = userAgent.ToLower();  
    return userAgent.Contains("facebookexternalhit");  
}  

Note:

Why would you need to do that? When you share a link to your site on Facebook, facebook crawls it and parses it to get some data to display the thumbnail, title and some content from your page, but it would link back to your site.

Also, I think this would lead to cloaking of the site, i.e. displaying different data to user and the crawlers. Cloaking is not considered a good practice and may search engines and site take note of it.

Update: Facebook also added a new useragent as of May 28th, 2014

Facebot

You can read more about the facebook crawler on https://developers.facebook.com/docs/sharing/webmasters/crawler


Please do note that sometimes the agent is visionutils/0.2 . You should check for it too.