How can a website server prevent CC/ddos attacks? php anti-attack code deployment

Summary of this articleWordpressAnti-CC attack, DDOS attack method.

How to protect website servers from CC/DDOS attacks? PHP anti-attack code deployment

The purpose of writing this article is because manyInternet marketingpersonnel useWordPress website.Websites are often subject to CC attacks/DDOS attacks, causing website servers to be overloaded and slow to load, affecting theirE-commerceWebsiteSEORanking.

At present, CC attack/DDOS attack mainly calls N multiple IPs, frequently refreshes a certain page of the website, so that the server requests are continuous, and finally causes the server to be overloaded and down.

WordPress server anti-CC, ddos ​​attack code deployment

  • The main function of the following code is to point the IP address that "refreshes the page more than 3 times or more continuously within 5 seconds" to the local IP address 127.0.0.1.
  • This method will be better to prevent CC and DDOS attacks.

WordPress website anti-CC/ddos, prevent malicious refresh code

Add the following code to the function.php file of the current WordPress theme▼

// Come form https://www.chenweiliang.com/
// WordPress防CC攻击代码,防恶意刷IP
session_start(); //开启session
$timestamp = time();
$ll_nowtime = $timestamp ;
//判断session是否存在 如果存在从session取值,如果不存在进行初始化赋值
if ($_SESSION){
$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;
}
//现在时间-开始登录时间 来进行判断 如果登录频繁 跳转 否则对session进行赋值
if(($ll_nowtime - $ll_lasttime) < 3){
if ($ll_times>=5){
header("location:http://127.0.0.1");
exit;
}
}else{
$ll_times = 0;
$_SESSION['ll_lasttime'] = $ll_nowtime;
$_SESSION['ll_times'] = $ll_times;
}

Website anti-CC, ddos ​​attack code (non-WordPress)

if notWordPress websitefriends, put the following code in the header, the effect will be better ▼

<?php
// Come form https://www.chenweiliang.com/
// WordPress防CC攻击代码,防恶意刷IP
session_start(); //开启session
$timestamp = time();
$ll_nowtime = $timestamp ;
//判断session是否存在 如果存在从session取值,如果不存在进行初始化赋值
if ($_SESSION){
$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;
}
//现在时间-开始登录时间 来进行判断 如果登录频繁 跳转 否则对session进行赋值
if(($ll_nowtime - $ll_lasttime) < 3){
if ($ll_times>=5){
header("location:http://127.0.0.1");
exit;
}
}else{
$ll_times = 0;
$_SESSION['ll_lasttime'] = $ll_nowtime;
$_SESSION['ll_times'] = $ll_times;
}
?>
  • Additionally, the above parameters can be modified, and the parameters can be modified as needed for better results.

Comment

Your email address will not be published. Required fields * Callout

Scroll to Top