Suppose a user came to the site and used the site search script, how do you restrict the use of this script with PHP? Those. so that when re-running the script, if 30 seconds had passed, he would get a message: wait a bit.
5 answers
Create a table with request. In which to store IP, and the time of the last visit. Extract IP from $ _SERVER. We are looking in the table, if there is no such append, if we compare the time of the last request with the current one, something like this
TIMESTAMPDIFF(SECOND, time_request, NOW())
If the time does not exceed the interval, update the field with the time of the last request, otherwise we give an error message.
If it is not critical, then it is better to assign a session to the cookie for 30 seconds in order not to create another trash on the hosting. And there is already a JS check to not create a load. About cookies here, for example: http://php.net/manual/ru/function.setcookie.php
and here is how to read the javascript cookie % 81-% D0% BA% D1% 83% D0% BA% D0% B8-% D0% B2-javascript-% D0% B8-jquery
- If cookies are used, they are easy to wipe. - Andrey Arshinov
My version:
Description of data:
cookie = { time, //ΠΡΠ΅ΠΌΡ ΠΏΡΠΎΡΠ»ΠΎΠ³ΠΎ ΡΡΠΏΠ΅ΡΠ½ΠΎΠ³ΠΎ Π·Π°ΠΏΡΠΎΡΠ° md5_time //md5( time + salt ) } //ΠΠ΅ΠΊΠΈΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ, Π΄ΠΎΠΏΡΡΡΠΈΠΌ memcached //ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ip - timestamp Π΄Π΅ΡΡΡΠΈ ΠΏΠΎΡΠ»Π΅Π΄Π½ΠΈΡ
Π²ΠΈΠ·ΠΈΡΠΎΠ² Π±Π΅Π· cookie mem_ip = { ip1: [ time1, time2, time3 ] }; //ΠΠ΅ΠΊΠΈΠΉ ΠΎΠ±ΡΠ΅ΠΊΡ, Π΄ΠΎΠΏΡΡΡΠΈΠΌ memcached //timestamp ΡΡΡΡΡΠΈ ΠΏΠΎΡΠ»Π΅Π΄Π½ΠΈΡ
Π²ΠΈΠ·ΠΈΡΠΎΠ² Π±Π΅Π· cookie mem_all = [ time1, time2, time3 ];
Algorithm
ΠΡΠ»ΠΈ ΠΊΡΠΊΠ° Π΅ΡΡΡ ΠΈ ΠΎΠ½Π° Π²Π΅ΡΠ½Π°Ρ Π’ΠΎΠ³Π΄Π° ΠΡΠ»ΠΈ ( curr_time - cookie.time ) > 30 Π’ΠΎΠ³Π΄Π°: ΠΠ±Π½ΠΎΠ²Π»ΡΠ΅ΠΌ ΠΊΡΠΊΡ ΠΡΠΎΠΏΡΡΠΊΠ°Π΅ΠΌ Π·Π°ΠΏΡΠΎΡ ΠΠ½Π°ΡΠ΅: ΠΠ»ΠΎΠΊΠΈΡΡΠ΅ΠΌ Π·Π°ΠΏΡΠΎΡ ΠΠ½Π°ΡΠ΅ //ΠΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π° DoS ΠΡΠ»ΠΈ count( mem_all, time > curr_time - 60 ) > 100 Π’ΠΎΠ³Π΄Π°: ΠΠ»ΠΎΠΊΠΈΡΡΠ΅ΠΌ Π·Π°ΠΏΡΠΎΡ //ΠΠ°ΡΠΈΡΠ° ΠΎΡ ΠΎΡΠΈΡΡΠΊΠΈ ΠΊΡΠΊΠΈ, Π½ΠΎ Π½Π΅Π±ΠΎΠ»ΡΡΠ°Ρ ΠΏΠΎΠ±Π»Π°ΠΆΠΊΠ° Π΄Π»Ρ NAT ΠΡΠ»ΠΈ count( mem_ip[ req_ip ], time > curr_time - 60 ) > 3 Π’ΠΎΠ³Π΄Π°: mem_all.push( curr_time ); mem_ip[ req_ip ].push( curr_time ); ΠΠ»ΠΎΠΊΠΈΡΡΠ΅ΠΌ Π·Π°ΠΏΡΠΎΡ ΠΠ½Π°ΡΠ΅: Π‘ΠΎΠ·Π΄Π°ΡΠΌ ΠΊΡΠΊΡ ΠΡΠΎΠΏΡΡΠΊΠ°Π΅ΠΌ Π·Π°ΠΏΡΠΎΡ
- The same memcached (for strangers with this) is replaced with a DB (as in the answer @ alter_f4), the excellent answer is Dex
- memcached faster than the database)) - timka_s
- I do not argue, therefore I say that for strangers with him - Dex
As an option :
- Create a file with the name containing its ip address in md5 and txt format.
- Check the creation time of this file, and if more than 30 seconds have passed since the creation, then allow, otherwise disable.
In principle, the easiest and fastest way.
Here is the code:
<?php $f_name = './gts/'.md5($_SERVER['REMOTE_ADDR']).'txt'; # if (file_exists($f_name) && (microtime(true) - filemtime($f_name) < 30) ) { echo '30 ΡΠ΅ΠΊΡΠ½Π΄ Π½Π΅ ΠΏΡΠΎΡΠ»ΠΎ, ΠΏΠΎΠ΄ΠΎΠΆΠ΄ΠΈΡΠ΅ Π½Π΅ΠΌΠ½ΠΎΠ³ΠΎ ΠΈ ΠΏΠΎΠ²ΡΠΎΡΠΈΡΠ΅ ΠΏΠΎΠΏΡΡΠΊΡ.'; } else { $fp = fopen($f_name, 'w'); fclose($fp); # //Π½ΡΠΆΠ½ΡΠΉ Π²Π°ΠΌ ΠΊΠΎΠ΄ echo 'ΠΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡΡΡ)'; } ?>
Do not forget to create a folder gts - it will create files.
- Can you imagine how many files there will be when at least the average load? - AlexDenisov
- fast? do you know that the bottleneck is working with FS?))) - timka_s
I support the variant with cookies and session, just write the time of the last request in cookies, and then compare it either via JS or already on the server. This option is better for at least two reasons:
- No need to store a bunch of extra information on the server.
- Several users coming from different machines, but through one gateway (having one external IP) will not interfere with each other.