PHP code example of gnikolovski / cb-rate-limiter

1. Go to this page and download the library: Download gnikolovski/cb-rate-limiter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gnikolovski / cb-rate-limiter example snippets


$app = new \Slim\App;

$app->add(function ($request, $response, $next) {
  $limiter = new CbRateLimiter($hostname, $bucket, $password);
  $limiter->whitelist($your_ip_address);
  $exceeded = $limiter->isExceeded($ip_address, $max_requests, $in_minutes);

  if (!$exceeded) {
    $resp = $next($request, $response)
      ->withHeader('X-RateLimit-Limit', 10)
      ->withHeader('X-RateLimit-Remaining', $limiter->getRemaining());
  }
  else {
    $resp = $response->withStatus(429)
      ->withHeader('X-RateLimit-Limit', 10)
      ->withHeader('X-RateLimit-Remaining', 0);
  }

  return $resp;
});

$app->get('/', function ($request, $response, $args) {
	$response->getBody()->write('Hello world');
	return $response;
});

$app->run();

$_SERVER['REMOTE_ADDR']

$limiter->isExceeded($_SERVER['REMOTE_ADDR'], 100, 60);