Created
November 10, 2017 17:11
-
-
Save smuuf/b4decc05e1da70db10cbf9a579d8706c to your computer and use it in GitHub Desktop.
Facebook Crawler Request Rate Limiter
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 | |
// Number of requests permitted for facebook crawler per second. | |
const FACEBOOK_REQUEST_THROTTLE = 20; | |
const FACEBOOK_REQUESTS_JAR = __DIR__ . '/.fb_requests'; | |
const FACEBOOK_REQUESTS_LOCK = __DIR__ . '/.fb_requests.lock'; | |
$ua = $_SERVER['HTTP_USER_AGENT'] ?? false; | |
if ($ua && strpos($ua, 'facebookexternalhit') !== false) { | |
$jar = @file(FACEBOOK_REQUESTS_JAR); | |
$currentTime = time(); | |
$timestamp = $jar[0] ?? time(); | |
$count = $jar[1] ?? 0; | |
if ($timestamp == $currentTime) { | |
$count++; | |
} else { | |
$count = 0; | |
} | |
file_put_contents(FACEBOOK_REQUESTS_JAR, "$currentTime\n$count"); | |
if ($count >= FACEBOOK_REQUEST_THROTTLE) { | |
header("HTTP/1.1 429 Too Many Requests", true, 429); | |
header("Retry-After: 60"); | |
die; | |
} | |
} | |
// Do other stuff... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment