Rate Limiter Middleware (PHP)
8. Rate Limiter Middleware (PHP)
===============================
function rateLimit($key, $maxRequests = 60, $window = 3600) {
$cacheKey = "rate:{$key}";
$count = (int) apcu_fetch($cacheKey) ?: 0;
if ($count >= $maxRequests) {
http_response_code(429);
die('Rate limit exceeded');
}
apcu_store($cacheKey, $count + 1, $window);
}
rateLimit($_SERVER['REMOTE_ADDR']); // Call before paste creation
Featured
Advertisement
Category
Action