Skip to content

Instantly share code, notes, and snippets.

@sergioccrr
Created January 3, 2016 14:51
Show Gist options
  • Save sergioccrr/5501ca2f71673e677199 to your computer and use it in GitHub Desktop.
Save sergioccrr/5501ca2f71673e677199 to your computer and use it in GitHub Desktop.
Check if a request is from Facebook Crawler
<?php
function check_facebook() {
$IP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
$UA = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : false;
if (!$IP || !$UA) return false;
$UAs = ['facebookexternalhit', 'Facebot', 'visionutils'];
$UAs = array_map('preg_quote', $UAs);
if (!preg_match('#^' . implode('|', $UAs) .'#i', $UA)) return false;
$output = [];
$command = 'whois -h whois.radb.net -- ' . escapeshellarg('-K ' . $IP);
exec($command, $output, $return_var);
if ($return_var !== 0 || count($output) !== 2) return false;
if (!preg_match('#^origin\:\s+AS32934$#', $output[1])) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment