-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
27 lines (21 loc) · 753 Bytes
/
Copy pathexample.php
File metadata and controls
27 lines (21 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
require_once __DIR__ . '/src/RateLimiter.php';
require_once __DIR__ . '/src/StorageInterface.php';
require_once __DIR__ . '/src/FileStorage.php';
use RateLimiterLibrary\RateLimiter;
use RateLimiterLibrary\FileStorage;
// Create storage folder if not exists
if (!is_dir(__DIR__ . '/storage')) {
mkdir(__DIR__ . '/storage', 0777, true);
}
$storage = new FileStorage(__DIR__ . '/storage');
$rateLimiter = new RateLimiter(5, 60, $storage); // 5 requests in 60 seconds
$ipAddress = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
if ($rateLimiter->isAllowed($ipAddress)) {
// Request is allowed
echo "Request successful.";
} else {
// Too many requests
http_response_code(429); // Too Many Requests
echo "Rate limit exceeded.";
}