Created
January 3, 2016 14:51
-
-
Save sergioccrr/5501ca2f71673e677199 to your computer and use it in GitHub Desktop.
Check if a request is from Facebook Crawler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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