K
K
Kirill Krasin2019-04-15 15:47:48
PHP
Kirill Krasin, 2019-04-15 15:47:48

How to limit the number of POST requests per IP using PHP or Apache?

There is a ready-made webhook that accepts POST requests from the specified IP addresses, when a request is received, the data is checked, after which they are entered into MySQL. IP addresses are entered by hand, but nevertheless, to fully automate the process, I would like to add a restriction. It is necessary to limit the number of requests to 1 in 10 minutes using PHP or Apache . Tell me how can I implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor Davydov, 2019-04-15
@Mo45

If you do not touch the database - you can use lock files!
The algorithm is approximately the following:
When a request is received, we try to open the file , i.e. the name is the client's IP ("89.108.13.56.lock", "127.0.0.1.lock", etc.) Then we read the Unix timestamp from this file, if 10 minutes have not passed - die(). If 10 minutes or more have passed, then we throw the current Unix timestamp into the file and work on! With a database, of course, it would be more correct, but since such a case, then you can use my method. $_SERVER['REMOTE_ADDR'] . ".lock"

I
irishmann, 2019-04-15
@irishmann

bd keep track of the time of the last POST. A new POST arrives, we request the time of the last POST from the database, compare it with the current one, if the time is less than 10 minutes, then the POST helmet is forested, otherwise we process

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question