PHP code example of beholdr / laravel-backoff-limiter

1. Go to this page and download the library: Download beholdr/laravel-backoff-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/ */

    

beholdr / laravel-backoff-limiter example snippets


use Beholdr\BackoffLimiter\BackoffLimiter;

$executed = app(BackoffLimiter::class)->attempt(
    'send-sms-'.request()->ip(),
    maxAttempts: 1,
    function () {
        // Send SMS
    }
)

if (! $executed) {
    throw new Exception('Too many requests!');
}

use Beholdr\BackoffLimiter\BackoffLimiter;

$limiter = app(BackoffLimiter::class);
$key = 'send-sms-'.request()->ip();

if ($limiter->tooManyAttempts($key, 1)) {
    throw new Exception('Too many requests!');
}

$limiter->hit($key)

// Send SMS

$limiter = app(BackoffLimiter::class, ['backoff' => 3*60*60, 'exponent' => 3]);
$limiter->hit(...);