Php cc attack prevention implementation code summary_php tutorial

Source: Internet
Author: User
PHP anti-CC attack implementation code summary. A cc attack means that the other party uses programs or some agents to continuously access your website. as a result, your website cannot be processed and is in the status of an on-premise server, next, we will summarize some CC attacks that prevent CC attacks, that is, the other party uses programs or some agents to continuously access your website. as a result, your website cannot be processed and it is in the status of a computer, the following is a summary of some php instance code against CC attacks. For more information, see.

Example 1

The code is as follows:

// Directly exit the proxy IP address
Empty ($ _ SERVER ['http _ vean']) or exit ('Access Denied ');
// Prevents quick refresh
Session_start ();
$ Seconds = '3'; // time period [seconds]
$ Refresh = '5'; // Number of Refreshes
// Set monitoring variables
$ Cur_time = time ();
If (isset ($ _ SESSION ['last _ time']) {
$ _ SESSION ['refresh_times '] + = 1;
} Else {
$ _ SESSION ['refresh_times '] = 1;
$ _ SESSION ['last _ time'] = $ cur_time;
}
// Process monitoring results
If ($ cur_time-$ _ SESSION ['last _ time'] <$ seconds ){
If ($ _ SESSION ['refresh_times ']> = $ refresh ){
// Jump to the attacker's server address
Header (sprintf ('Location: % s', 'http: // 127.0.0.1 '));
Exit ('Access Denied ');
}
} Else {
$ _ SESSION ['refresh_times '] = 0;
$ _ SESSION ['last _ time'] = $ cur_time;
}

Example 2

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;
}

I personally tested an instance

Log Analysis

[03:03:13] [client 61.217.192.39]/index. php
[03:03:13] [client 61.217.192.39]/index. php
[03:03:13] [client 61.217.192.39]/index. php
[03:03:13] [client 61.217.192.39]/index. php
[03:03:12] [client 61.217.192.39]/index. php
[03:03:12] [client 61.217.192.39]/index. php
[03:03:12] [client 61.217.192.39]/index. php
[03:03:11] [client 61.217.192.39]/index. php
[03:03:11] [client 61.217.192.39]/index. php
[03:03:11] [client 61.217.192.39]/index. php
[03:03:10] [client 61.217.192.39]/index. php
[03:03:10] [client 61.217.192.39]/index. php

The following is the PHP method: save the following code as a PHP file, and then include the first line into your common. PHP file.

The code is as follows:

/*
* CC attack prevention is depressing.
*
* If the number of refresh times of the website exceeds 2 times per second, the website will be accessed after 5 seconds.
*/

$ Cc_min_nums = '1'; // times, refresh count
$ Cc_url_time = '5'; // second, delay time
// $ Cc_log = 'cc_log.txt '; // enable this behavior log
$ Cc_forward = 'http: // localhost'; // release to URL

//--------------------------------------------

// Return URL
$ Cc_uri = $ _ SERVER ['request _ URI ']? $ _ SERVER ['request _ URI '] :( $ _ SERVER ['php _ SELF']? $ _ SERVER ['php _ SELF ']: $ _ SERVER ['script _ name']);
$ Site_url = 'http: // '. $ _ SERVER ['http _ host']. $ cc_uri;

// Enable session
If (! Isset ($ _ SESSION) session_start ();
$ _ SESSION ["visiter"] = true;
If ($ _ SESSION ["visiter"] <> true ){
Echo "script setTimeout (" window. location. href = '$ cc_forward'; ", 1); script";
// Header ("Location:". $ cc_forward );
Exit;
}

$ Timestamp = time ();
$ Cc_nowtime = $ timestamp;
If (session_is_registered ('CC _ lasttime ')){
$ Cc_lasttime = $ _ SESSION ['CC _ lasttime'];
$ Cc_times = $ _ SESSION ['cc_times '] + 1;
$ _ SESSION ['cc_times '] = $ cc_times;
} Else {
$ Cc_lasttime = $ cc_nowtime;
$ Cc_times = 1;
$ _ SESSION ['cc_times '] = $ cc_times;
$ _ SESSION ['CC _ lasttime'] = $ cc_lasttime;
}

// Obtain the real IP address
If (isset ($ _ SERVER )){
$ Real_ip = $ _ SERVER ['http _ X_FORWARDED_FOR '];
} Else {
$ Real_ip = getenv ("HTTP_X_FORWARDED_FOR ");
}

// Print_r ($ _ SESSION );

// Release the IP address
If ($ cc_nowtime-$ cc_lasttime) <= 0 ){
If ($ cc_times >=$ cc_min_nums ){
If (! Empty ($ cc_log) cc_log (get_ip (), $ real_ip, $ cc_log, $ cc_uri); // Generate a log
Echo "Wait please, try again later! Script setTimeout ("window. location. href = '$ site_url';", 5000); script ";
// Printf ('You refresh too fast, please wait. ');
// Header ("Location:". $ cc_forward );
Exit;
}
} Else {
$ Cc_times = 0;
$ _ SESSION ['CC _ lasttime'] = $ cc_nowtime;
$ _ SESSION ['cc_times '] = $ cc_times;
}

// Record cc logs
Function cc_log ($ client_ip, $ real_ip, $ cc_log, $ cc_uri ){
$ Temp_time = date ("Y-m-d H: I: s", time () + 3600*8 );

$ Temp_result = "[". $ temp_time. "] [client". $ client_ip. "]";
If ($ real_ip) $ temp_result. = "[real". $ real_ip. "]";
$ Temp_result. = $ cc_uri. "rn ";

$ Handle = fopen ("$ cc_log", "rb ");
$ Oldcontent = fread ($ handle, filesize ("$ cc_log "));
Fclose ($ handle );

$ Newcontent = $ temp_result. $ oldcontent;
$ Fhandle = fopen ("$ cc_log", "wb ");
Fwrite ($ fhandle, $ newcontent, strlen ($ newcontent ));
Fclose ($ fhandle );
}

// Obtain the online IP address
Function get_ip (){
Global $ _ C;

If (empty ($ _ C ['Client _ IP']) {
If (getenv ('http _ CLIENT_IP ') & strcasecmp (getenv ('http _ CLIENT_IP'), 'Unknown ')){
$ Client_ip = getenv ('http _ CLIENT_IP ');
} Elseif (getenv ('http _ X_FORWARDED_FOR ') & strcasecmp (getenv ('http _ X_FORWARDED_FOR'), 'Unknown ')){
$ Client_ip = getenv ('http _ X_FORWARDED_FOR ');
} Elseif (getenv ('remote _ ADDR ') & strcasecmp (getenv ('remote _ ADDR'), 'Unknown ')){
$ Client_ip = getenv ('remote _ ADDR ');
} Elseif (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], 'Unknown ')){
$ Client_ip = $ _ SERVER ['remote _ ADDR '];
}
$ _ C ['Client _ IP'] = $ client_ip? $ Client_ip: 'Unknown ';
}
Return $ _ C ['Client _ IP'];
}
?>

In this way, the basic industry can be prevented, but if more advanced accounts cannot be used, you can try to use the hardware to set the strong fire resistance.

...

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.