How to use PHP to prevent CC attacks

Source: Internet
Author: User
How to use PHP to prevent CC attacks attackers can use the proxy server to generate valid requests to the affected host to implement DOS and disguise cc (ChallengeCollapsar ). The principle of CC attack is that attackers control some hosts to repeatedly send a large number of packets to the other server, causing server resource depletion until the server crashes. How to use PHP to prevent CC attacks

Attackers can use the proxy server to generate valid requests to the affected host to implement DOS and disguise them as ChallengeCollapsar ).

The principle of CC attack is that attackers control some hosts to repeatedly send a large number of packets to the other server, causing server resource depletion until the server crashes. CC is mainly used to attack pages. everyone has this experience: when a webpage has a large number of visitors, it will slow down to open the webpage, CC simulates multiple users (the number of threads is the number of users) to constantly access those pages that require a large amount of data operations (that is, a large amount of CPU time), resulting in a waste of server resources, when the CPU remains at 100% for a long time, there will always be endless connections until network congestion occurs and normal access is terminated.

The so-called CC attack means that the other party uses programs or some agents to continuously access your website, resulting in your website being unable to handle and being in the status of a computer. In this case, your statistical system (which may be quantum or Baidu) cannot be used. However, we can use some anti-attack software, but the effect is sometimes not obvious. The following code is provided by longminhong to prevent CC attacks.

Especially for users who use virtual hosts, if the CPU is overloaded, a "service unariable" prompt will appear. There are many factors related to overloading, such as unreasonable webpage structure and excessive traffic. Another possible malicious factor is CC attacks.

Main function: Refresh the page more than 5 times in 3 seconds and direct it to the local http: // 127.0.0.1


 

The code is as follows:

$ P_S_T = $ t_array [0] + $ t_array [1];
$ Timestamp = time ();

Session_start ();
$ Ll_nowtime = $ timestamp;
If (session_is_registered ('ll _ lasttime ')){
$ Ll_lasttime = $ _ SESSION ['ll _ lasttime'];
$ Ll_times = $ _ SESSION ['ll _ times '] + 1;
$ _ SESSION ['ll _ times '] = $ ll_times;
} Else {
$ Ll_lasttime = $ ll_nowtime;
$ Ll_times = 1;
$ _ SESSION ['ll _ times '] = $ ll_times;
$ _ SESSION ['ll _ lasttime '] = $ ll_lasttime;
}
If ($ ll_nowtime-$ ll_lasttime) <3 ){
If ($ ll_times> = 5 ){
Header (sprintf ("Location: % s", 'http: // 127.0.0.1 '));
Exit;
}
} Else {
$ Ll_times = 0;
$ _ SESSION ['ll _ lasttime'] = $ ll_nowtime;
$ _ SESSION ['ll _ times '] = $ ll_times;
}

3 indicates the time interval, and 5 indicates the number of refreshes.

The above code can be stored in the inclusion file of each php file, so that each page can be protected against CC attacks. You can also set the parameters according to your own situation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.