Rate Limiter Middleware (PHP)

Text Views: 76 • Feb 08, 2026 • Never expires
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
🛡️

Ad Blocker Detected

PasteBin.site is free to use thanks to our sponsors. Please consider disabling your ad blocker or whitelisting our site to support our servers.

Learn More