PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Nitesh Apte   Rate Limiter PHP   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Rate Limiter PHP
Delay a request response to limit the access rate
Author: By
Last change:
Date: 23 days ago
Size: 586 bytes
 

Contents

Class file image Download
<?php
require_once 'rate_limiter_config.php';

/** @var RateLimiter $rateLimiter */
$rateLimiter = require 'rate_limiter_config.php';

// Simulate requests
for ($i = 0; $i < 10; $i++) {
    if (
$rateLimiter->allowRequest()) {
        echo
"Request $i: Allowed\n";
    } else {
        echo
"Request $i: Rate limit exceeded\n";
        if (
$rateLimiter->waitForRequest()) {
            echo
"Request $i: Allowed after waiting\n";
        } else {
            echo
"Request $i: Denied after timeout\n";
        }
    }

   
usleep(200000); // Simulate delay between requests (200ms)
}
?>