PHP Classes

How a PHP Rate Limiting Library Class Can Slow Down a Site User's Accesses Using the Package Rate Limiter PHP: Delay a request response to limit the access rate

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-12-08 (9 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 35 This week: 2All time: 11,025 This week: 27Up
Version License PHP version Categories
rate-limiter-php 1.0.2GNU General Publi...5Security, Performance and optimization, P...
Description 

Author

This class can delay a request response to limit the access rate.

It can note the current time and wait for a given period before proceeding.

The time delay that the class waits is a configurable value.

Innovation Award
PHP Programming Innovation award nominee
November 2024
Nominee
Vote
If a site gets too many users accessing the site, it may overload the server and all users stop being served of the site pages.

If a user needs to wait a given amount of time before the site server sends the page's content to the user's browser, the server load may be lower.

This package delays the response to each user request to pages served by the current PHP script.

This way the served load can be limited to avoid overloading.

Manuel Lemos
Picture of Nitesh Apte
  Performance   Level  
Name: Nitesh Apte <contact>
Classes: 19 packages by
Country: India India
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Example

<?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)
}
?>


Details

Rate Limiter in PHP

A pure PHP implementation of a rate limiter.

Blog post - https://blog.niteshapte.com/2024-12-01-rate-limiter-in-php.htm

Features

  • Limit the number of requests within a specific time period.
  • Timeout mechanism for requests exceeding the limit.
  • Customizable parameters.

Installation

Clone the repository:

git clone https://github.com/niteshapte/rate-limiter-php.git

Usage

Configuration

The rate limiter can be configured in `rate_limiter_config.php`

$rateLimiter = new RateLimiter(
    limitForPeriod: 2,         // Max 2 requests
    limitRefreshPeriod: 1.0,   // Reset limit every 1 second
    timeoutDuration: 0.1       // Timeout after 500 milliseconds
);

Example

Check the `example.php` file:

<?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)
}
?>

  Files folder image Files (5)  
File Role Description
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Plain text file RateLimiter.php Class Class source
Accessible without login Plain text file rate_limiter_config.php Conf. Configuration script
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:35
This week:2
All time:11,025
This week:27Up