-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate-limit-rqs.php
67 lines (65 loc) · 2.7 KB
/
rate-limit-rqs.php
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
//Rate Limit Request
function rate_limit_request()
{
if (!class_exists("Redis") and !class_exists("Memcached")) {
echo '<br><h1>Important Warning:</h1>
<h2>You need "Redis" or "Memcached" in PHP extension for activation.</h2>
<br>';
}
$numberof_request = 5; //number of request
$requestin_period = 10; // per seconds
$timeof_block = 60; //blocking time
//ex: if an ip send 5 request in 10s , blocking for 60 s.
function blocked_page($ip)
{
echo '
<br><p style="text-align:center"><strong><span style="font-size:16px;font-family:Courier New,Courier">Your request (' .
$ip .
') is temporarily blocked! Wait a few seconds and then try again.</span></strong></p>
<p style="text-align:center"> </p>
<p style="text-align:center"><span style="font-size:16px;font-family:Courier New,Courier">Powered by <a href="https://github.com/Jhonvalta/prevent_IP_Stresser" target="_blank">Rate Limit Request</a></span></p>';
}
$total_user_calls = 0;
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$user_ip_address = $_SERVER["HTTP_CLIENT_IP"];
} elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
$user_ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
$user_ip_address = $_SERVER["REMOTE_ADDR"];
}
if (class_exists("Redis")) {
$redis = new Redis();
$redis->connect("localhost", 6379);
if (!$redis->exists($user_ip_address)) {
$redis->set($user_ip_address, 1);
$redis->expire($user_ip_address, $requestin_period);
$total_user_calls = 1;
} else {
$redis->INCR($user_ip_address);
$total_user_calls = $redis->get($user_ip_address);
if ($total_user_calls > $numberof_request) {
blocked_page($user_ip_address);
$redis->set($user_ip_address, $numberof_request);
$redis->expire($user_ip_address, $timeof_block);
exit();
}
}
} elseif (class_exists("Memcached")) {
$memc = new Memcached();
$memc->addServer("localhost", 11211);
$item = $memc->get($user_ip_address);
if ($memc->getResultCode() == Memcached::RES_SUCCESS) {
$total_user_calls = $memc->get($user_ip_address);
$total_user_calls = $total_user_calls + 1;
$memc->replace($user_ip_address, $total_user_calls);
if ($total_user_calls > $numberof_request) {
blocked_page($user_ip_address);
$memc->set($user_ip_address, $numberof_request, $timeof_block);
exit();
}
} else {
$memc->set($user_ip_address, 1, $requestin_period);
}
}
}