PHP code example of genai / rate-limit

1. Go to this page and download the library: Download genai/rate-limit 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/ */

    

genai / rate-limit example snippets


   #[Configuration]
   class RateLimitConfig {
       #[Bean(RateLimiter::class)]
       public function rateLimiter(RateLimitProperty $cfg) {
           return RateLimitFactory::build($cfg);
       }
   }
   

   #[Intercept]   // all requests; only POST-likes are counted
   class Throttle extends \GenAI\RateLimit\Interceptor\RateLimitInterceptor {}
   

#[Bean(AttemptLimiter::class)]
public function loginLockout(RateLimitProperty $cfg) {
    return RateLimitFactory::lockout($cfg);   // [ratelimit] login_max_fails / login_lock
}

if (($wait = $lockout->lockedFor($email)) > 0) {
    return "locked — try again in ~" . ceil($wait / 60) . " min";
}
if ($passwordOk) { $lockout->clear($email); }   // reset on success
else             { $lockout->fail($email);  }    // count the failure