Home  >  Article  >  Backend Development  >  PHP prevents CC attack code PHP prevents frequent refresh of web pages

PHP prevents CC attack code PHP prevents frequent refresh of web pages

不言
不言Original
2018-06-02 14:11:532014browse

This article mainly introduces the code of PHP to prevent CC attacks and PHP to prevent frequent refresh of web pages, rapid and malicious refresh of web pages. CC attacks use proxy servers to generate legitimate requests directed to the target site, simulating multiple users to continuously access the victim website. , interested friends can study

Rapid malicious refresh of web pages, cc attack is that the attacker uses the proxy server to generate legitimate requests directed to the target site, simulating multiple users to continuously access the victim website, especially Accessing pages that require a large amount of data operations and a large amount of CPU time will eventually lead to the target website server resources being exhausted until it crashes. In this way, server resources are wasted, and the CPU is at 100% for a long time, and there is always processing. Endless connections occur until the network is congested and normal access is terminated. We call it a CC attack. Although we can use some anti-attack software to achieve this, the effect is sometimes not obvious.

Below I provide a piece of PHP code that can prevent CC attacks to a certain extent.
Main functions: Continuously refreshing the page more than 5 times within 3 seconds will point to the local http://127.0.0.1
Just put this code on the page that needs to prevent CC attacks. Can.
Source code

=5){
 echo '刷新太快!';
 exit;
 }
}else{
 $cc_times = 0;
 $_SESSION['cc_lasttime'] = $cc_nowtime;
 $_SESSION['cc_times'] = $cc_times;
} 
?>

The following is an effective method for preventing CC attacks in PHP for everyone:

1 .session record
submit.php is the sending page. Set a session variable on this page and send it to the submitdeal.php page as a hidden field and form. On the server side, the hidden variable posted from the post is sent to the server. Compare the session variables recorded on the end. For example, if they are the same, write to the database and clear the session. In this way, when the user refreshes the page, if the two values ​​are not equal, an error or jump will be prompted.
Advantages: No need for users to enter verification codes
Disadvantages: The form is easily copied
2 .Verification code
The principle is the same as the first one, except that the session data is not submitted as a hidden field, but is filled in by the user. Most websites use text verification codes and picture verification codes, and picture verification codes are highly secure. .

3.IP binding
After submitting the data, first retrieve the client IP from the IP table. If there is one and it has not expired, then an error will be reported, otherwise it will be written to the database, and then Then get the guest room IP and write the IP into the database.
4.cookie:After the client submits, the processing program first searches whether the client has set a cookie. If so, it will not submit again; if not, it will write the data and then write a cookie;
Example:

Users' meaningless frequent jumps and requests will put a lot of burden on the server. In fact, using cookies can prevent this.

Method one:

$allowTime){  
 $refresh = true;  
 $_SESSION[$allowT] = time();  
}else{  
 $refresh = false;  
}  
?>

Method two:

 

Method three:

 

The above is php There are many ways to prevent CC attacks. I hope it can help everyone prevent web pages from being refreshed quickly and maliciously.

Related recommendations:

ThinkPHP’s method to prevent repeated submission of forms

Explanation of PHP’s method to prevent sql injection

The above is the detailed content of PHP prevents CC attack code PHP prevents frequent refresh of web pages. 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