Detect Facebook as referrer?

An Phan picture An Phan · Aug 17, 2013 · Viewed 8.8k times · Source

I have this closed-beta website that disables normal visits - visitors have to log in first to view the site. I have a secret Facebook group also, in which we often share the links to the website articles (for discussion). Problem is, when we paste a link, Facebook tries to get its preview and fails, since it's considered a non-logged in user. So in short, I'd want the website is public only to Facebook. These are the solutions I've come up with:

  1. Detect the referrer: Fails, the referrer is empty - maybe because FB browses the site from HTTPs?
  2. Detect client IP: Not sure about this. Does FB server have a fixed IP address?
  3. Search about a "Facebook query string" that FB uses when browses my site - something like mysite.com/?from=I'm-Facebook-Bitch: No results so far.

What do you think? Is there a reliable way to accomplish this mission? Thanks in advance.

Answer

An Phan picture An Phan · Aug 17, 2013

OK I've found the solution here: https://developers.facebook.com/docs/reference/plugins/like/

Instead of checking for IP or referrer, we can check for the user agent ($_SERVER['HTTP_USER_AGENT']) of the FB scraper. The value is facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)

Hope it helps someone else.