This function is used to count the total number of visits per visitor in a short period of time. if the limit is exceeded, TRUE is returned, then you can use PHP to call linux iptables for blocking.
I have used several DDOS tools for actual testing, and the results are very good.
By the way, I use a file in the code to record the ip address and time of the visitor. it is best not to use a database (or store it in the session smartly ), in addition, it is best to put this file on an SSD hard disk. I won't say the reason. we all know it.
- /**
- Prevents the customer receiver from being intentionally reorganized
- Usage:
- $ Isf5 = Fun: isf5 ();
- Return value:
- Return bool [true: the operator indicates the reorganization in intent; false: normal response]
- /**/
- Public static function isf5 (){
- $ _ F = Run. '_ isf ';
- If (! File_exists ($ _ f )){
- File_put_contents ($ _ f, serialize (array (), LOCK_EX );
- Chmod ($ _ f, 0777 );
- }
- $ Arr = unserialize (file_get_contents ($ _ f ));
- $ Arr = (! Is_array ($ arr ))? Array (): $ arr;
- // Clear the token used for the last 10 seconds.
- Foreach ($ arr as $ k =>$ v ){
- If ($ _ ENV ['right']-$ v ['t']> = 10 ){
- Unset ($ arr [$ k]);
- }
- }
- $ Ip = '_'. (self: cur ('IP '));
- If (! Isset ($ arr [$ ip]) {
- $ Arr [$ ip] ['n'] = 1; // The number of batch requests in 1 S
- $ Arr [$ ip] ['t'] = $ _ ENV ['right']; // the time of the first 1st million Reply requests
- File_put_contents ($ _ f, serialize ($ arr), LOCK_EX );
- Chmod ($ _ f, 0777 );
- Unset ($ _ f, $ arr, $ ip );
- Return FALSE;
- } Else {
- If (! Isset ($ arr [$ ip] ['t']) or! Is_numeric ($ arr [$ ip] ['t']) {
- Unset ($ arr [$ ip]);
- File_put_contents ($ _ f, serialize ($ arr), LOCK_EX );
- Chmod ($ _ f, 0777 );
- Unset ($ _ f, $ arr, $ ip );
- Return FALSE;
- }
- If ($ _ ENV ['right']-$ arr [$ ip] ['t']) <= 1) {// if there is not more than 1 s since the time when the last response was asked by the queue, only the number of requests is accumulated.
- $ Arr [$ ip] ['n'] + = 1;
- If ($ arr [$ ip] ['n']> = 5 ){
- Unset ($ _ f, $ arr, $ ip );
- Return TRUE;
- } Else {
- File_put_contents ($ _ f, serialize ($ arr), LOCK_EX );
- Chmod ($ _ f, 0777 );
- Unset ($ _ f, $ arr, $ ip );
- Return FALSE;
- }
- } Else {// if the time from the last time the queue was asked has exceeded 1 s, the data will be recalculated.
- $ Arr [$ ip] ['n'] = 1;
- $ Arr [$ ip] ['t'] = $ _ ENV ['right'];
- File_put_contents ($ _ f, serialize ($ arr), LOCK_EX );
- Chmod ($ _ f, 0777 );
- Unset ($ _ f, $ arr, $ ip );
- Return FALSE;
- }
- }
- Unset ($ _ f, $ arr, $ ip );
- Return FALSE;
- }
|