Home  >  Article  >  Backend Development  >  Using Redis to implement RateLimiter in PHP

Using Redis to implement RateLimiter in PHP

王林
王林Original
2023-05-19 21:01:341368browse

RateLimiter is one of the basic components of flow control, used to control access frequency, protect the server from malicious attacks, and prevent overload.

In PHP, we can use Redis as data storage to implement a simple and efficient RateLimiter. This article will introduce how to use Redis to implement RateLimiter.

  1. Install Redis extension

First, we need to install the Redis extension. In a Linux system, you can use the following command to install:

sudo apt-get install php-redis

If you are using a Windows system, you can get the extension from PECL and compile and install it manually.

  1. Initialize Redis connection

In PHP, using Redis requires the class provided by the phpredis extension. We need to initialize a Redis connection object.

$redis = new Redis(); // 创建一个 Redis 对象
$redis->connect('127.0.0.1', 6379); // 连接到 Redis

Here we assume that Redis is running locally and uses the default port 6379. If Redis is running on another server or port, the corresponding parameters need to be modified.

  1. Implementing RateLimiter

Below, we will implement a simple RateLimiter that limits each IP address to a maximum of 100 visits per hour.

// 获取客户端IP地址
$clientIp = $_SERVER['REMOTE_ADDR'];
// Redis key,将客户端IP地址与当前小时数拼接在一起,作为唯一的key
$redisKey = $clientIp . '_' . date('Y-m-d-H');

// 从Redis中获取当前IP地址在当前小时数内已经访问的次数
$count = $redis->get($redisKey);
if ($count === false) { // 如果之前没有访问记录,则设置为0
    $redis->set($redisKey, 0);
    $redis->expire($redisKey, 3600); // 设置过期时间为1小时
}

if ($count >= 100) { // 如果当前IP地址在当前小时数内已经访问超过100次,则返回错误
    header('HTTP/1.1 429 Too Many Requests');
    exit;
}

$redis->incr($redisKey); // 访问次数加1

In the code, we use the get, set, expire and incr methods of Redis. get is used to get the number of times the current IP address has been accessed within the current hour, set is used to initialize the number of accesses to 0 and set the expiration time to 1 hour, expire is used to set the expiration time to prevent abuse of IP from occupying Redis memory, incr The number of visits used to operate the current IP address is increased by 1.

  1. Summary

Using Redis to implement RateLimiter is a simple and efficient way to effectively protect the server from malicious attacks and prevent overload. The classes provided by the phpredis extension make it extremely simple to use Redis in PHP. We only need to simply configure the connection.

Of course, the examples here are just simple demonstrations and do not consider some special situations, but you can modify the code as needed to meet your needs.

The above is the detailed content of Using Redis to implement RateLimiter in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn