How to recognize a Facebook agent

When I share one of my pages on FB, I want to show something else. The problem is that I prefer not to use og: elements, but to recognize the FB user agent.

What is it? I can not find it.

+53
facebook user-agent
Dec 24 2018-11-12T00:
source share
11 answers

For a list of user agent strings, search here . The most used as of September 2015 are facebookexternalhit/* and Facebot . Since you did not indicate in which language you are trying to recognize the user agent, I cannot tell you more information. If you want to recognize the Facebook bot in PHP, use

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

UPDATE . Facebook added Facebot to its list of possible user agent strings, so I updated my code to reflect this change. In addition, the code is now more predictable for possible future changes.

+103
Dec 24 2018-11-12T00:
source share

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

Hello

A small but important fix β†’ Appearance Facebook 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 the feeder to 1.1 can lead to filtering problems with version 1.0.

For more information on Facebook Bot (and other bots), please refer to Botopedia.org , a directory of bots owned by Comunity-Sourced, based on Incapsula.

In addition to user agent data, the directory also offers an IP address verification option that allows cross-checking of the IP / User-Agent, thereby helping to prevent impersonation attempts.

+16
Aug 14 2018-12-14T00:
source share

Here is the Google 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) 

Please note that version numbers are subject to change. So use a regular expression to find the name of the crawler, and then display it.

Update :

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

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

Here is the ASP.NET code. You can use this function to check if userAgent is a Facebook user agent.

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

Note:

Why do you need this? When you share a link to your site on Facebook, facebook looks at it and analyzes it to get some data to display a thumbnail, title and some content from its page, but it will link to your site.

In addition, I think that this will lead to a masking of the site, that is, the display of different data for the user and scanners. Cloaking is not considered good practice and may look for engines and a site to take note of.

Update . Facebook has also added a new user account since May 28, 2014.

 Facebot 

You can learn more about facebook finder at https://developers.facebook.com/docs/sharing/webmasters/crawler

+14
Dec 24 '11 at 9:01
source share

Note that sometimes the agent is visionutils/0.2 . You must check this too.

+4
May 18 '14 at 8:25
source share

A quick solution is to check the pattern, not loading each clutter to the user every time

 <?php # Facebook optimized stuff if(strstr($_SERVER['HTTP_USER_AGENT'],'facebookexternalhit')) { $buffer.='<link rel="image_src" href="images/site_thumbnail.png" />'; } ?> 
+3
Apr 13 '13 at 12:23
source share

Firstly, you should not use in_array, since you will need to have a full user agent, not just a subset, so it will break quickly with changes (for example, version 1.2 from facebook will not work if you follow the current preferred answer). It is also slower to iterate through an array rather than using a regex pattern.

As without a doubt, you will want to look more for a bot, so I gave an example below with two bot names divided into a template using pipe | symbol. / i at the end makes the case insensitive.

Also you should not use $ _SERVER ['HTTP_USER_AGENT']; but you should filter it first if someone was a little unpleasant.

 $pattern = '/(FacebookExternalHit|GoogleBot)/i'; $agent = filter_input(INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_ENCODED); if(preg_match($pattern,$agent)){ echo "found one of the patters"; } 

More secure and faster code.

+1
Jan 6 '14 at 11:38
source share

And if you want to block facebook bots from accessing your site (assuming you are using Apache), add this to your .htaccess file:

 <Limit GET POST> BrowserMatchNoCase "Feedfetcher-Google" feedfetcher BrowserMatchNoCase "facebookexternalhit" facebook order deny,allow deny from env=feedfetcher deny from env=facebook </Limit> 

It also blocks Google FeedFetcher, which can also be used for cheap DDoSing.

+1
Apr 27 '14 at 10:30
source share

In the long run of user agent modifications on the FB side, it may be safer to use this regular expression:

 <?php if (preg_match("/facebook|facebot/i", $_SERVER['HTTP_USER_AGENT'])){ do_something(); } ?> 

For more information on the Facebook crawler, see your document: https://developers.facebook.com/docs/sharing/webmasters/crawler

+1
Apr 29 '15 at 8:36
source share

Facebook user agents:

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

I use the code below to detect the FB User-Agent in PHP, and it works as intended:

 $agent = $_SERVER['HTTP_USER_AGENT']; if(stristr($agent, 'FacebookExternalHit')){ //Facebook User-Agent }else{ //Other User-Agent } 
+1
May 7, '15 at 1:38
source share

Another common approach in PHP

 $agent = $_SERVER['HTTP_USER_AGENT']; $agent = trim($agent); $agent = strtolower($agent); if ( strpos($agent,'facebookexternalhit/1.1')===0 || strpos($agent,'facebookexternalhit/1.0')===0 ){ //probably facebook }else{ //probably not facebook } 
0
Mar 13 '14 at 10:09
source share

You already have the answer for Facebook above, but one way to get any user agent is to place a script on your site that will send you emails when you visit it. For example, create this file in your domain, for example, at https://example.com/user-agent.php :

 <?php mail('you@youremail.com', 'User Agent', $_SERVER['HTTP_USER_AGENT']); 

Then go to Facebook, enter the link to the script there and press the spacebar. You really don't need to share anything, just by typing a link and a space will make Facebook get a preview. Then you should receive an email with your Facebook user agent.




Enter the link on Facebook

Get an email with the user agent

0
Jul 16 '19 at 8:56
source share



All Articles