Home Backend Development PHP Tutorial php防止CC攻击代码 php防止网页频繁刷新_php技巧

php防止CC攻击代码 php防止网页频繁刷新_php技巧

May 16, 2016 pm 08:02 PM
CC attack php

网页快速恶意刷新,cc攻击就是攻击者利用代理服务器生成指向目标站点的合法请求,模拟多用户不停的对受害网站进行访问,特别是访问那些需要大量数据操作需要大量CUP时间的页面,最终导致目标网站服务器资源耗尽,一直到宕机崩溃,如此一来,造成服务器资源的浪费,CPU长时间处于100%,永远都有处理不完的连接直至就网络拥塞,正常的访问被中止。我们称之为CC攻击。尽管我们可以借助于一些防攻击的软件来实现,不过效果有时并不明显。

下面我提供一段PHP的代码,可以起到一定的防CC攻击效果。
主要功能:在3秒内连续刷新页面5次以上将指向本机 http://127.0.0.1 
只需将该代码放到需要防CC攻击的页面即可。
源码

<&#63;php
$timestampcc = time();
$cc_nowtime = $timestampcc;
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;
} 
if(($cc_nowtime-$cc_lasttime)<3){//3秒内刷新5次以上可能为cc攻击
 if($cc_times>=5){
 echo '刷新太快!';
 exit;
 }
}else{
 $cc_times = 0;
 $_SESSION['cc_lasttime'] = $cc_nowtime;
 $_SESSION['cc_times'] = $cc_times;
} 
&#63;>
Copy after login

下面是为大家整理的PHP防CC攻击的有效方法:

1.session记录
submit.php为发送页面,在这个页面上设置一个session变量,并作为隐藏域和表单一起发送到submitdeal.php页面,在服务器端把post上来的隐藏变量和服务器端记录的session变量进行对比,比如一样,则写入数据库并清除session,这样用户刷新页面,两个值不相等提示错误或跳转。
优点:不用用户输入验证码
缺点:表单容易被复制
2.验证码
原理和第一种一样,只是session数据不作为隐藏域提交,而是让用户填写,大多数网站都采用文字验证码和图片验证码,图片验证码安全性高。

3.IP绑定
提交数据后,先从IP表里检索客户端IP,如果有,并且没有过期,那么报错,否则写入数据库,然后再取客房端IP,把IP写入数据库。
4.cookie:客户提交后处理程序先检索客户端有没有设置cookie,如果有,则不重复提交;如果没有,则写数据,再写个cookie;
例:

用户无意义的频繁跳转、请求都会给服务器加重很多负担 其实 用cookie就可以防止这一点。

<&#63;php 
error_reporting(0); 
//if($_COOKIE["ck"])die("刷新过快!"); 
if($_COOKIE["ck"])header("Location:http://www.baidu.com");//这里如果用户刷新过快,给予终止php脚本或者直接302跳转 
setcookie("ck","1",time()+3);//设定cookie存活时间3s 
echo "hello!"; 
&#63;>
Copy after login

方式一:

<&#63;php  
session_start();  
$k=$_GET['k'];  
$t=$_GET['t'];  
$allowTime = 1800;//防刷新时间  
$ip = get_client_ip();  
$allowT = md5($ip.$k.$t);  
if(!isset($_SESSION[$allowT]))  
{  
 $refresh = true;  
 $_SESSION[$allowT] = time();  
}elseif(time() - $_SESSION[$allowT]>$allowTime){  
 $refresh = true;  
 $_SESSION[$allowT] = time();  
}else{  
 $refresh = false;  
}  
&#63;> 
Copy after login

方式二:

<&#63; 
session_start(); 
if(!emptyempty($_POST[name])){ 
 $data = $_POST[name]; 
 $tag = $_POST[tag]; 
 if($_SESSION[status]==$tag){ 
 echo $data; 
 }else{ 
 echo "不允许刷新!"; 
 } 
} 
$v = mt_rand(1,10000); 
&#63;> 
<form method="post" name="magic" action="f5.php"><input type="hidden" 
name="tag" value="<&#63;=$v&#63;>"><input type=text name="name"><input type="submit" value="submit"> 
</form> 
<&#63; 
echo $v; 
$_SESSION[status] = $v; 
&#63;> 
Copy after login
Copy after login

方式三:

<&#63; 
session_start(); 
if(!emptyempty($_POST[name])){ 
 $data = $_POST[name]; 
 $tag = $_POST[tag]; 
 if($_SESSION[status]==$tag){ 
 echo $data; 
 }else{ 
 echo "不允许刷新!"; 
 } 
} 
$v = mt_rand(1,10000); 
&#63;> 
<form method="post" name="magic" action="f5.php"><input type="hidden" 
name="tag" value="<&#63;=$v&#63;>"><input type=text name="name"><input type="submit" value="submit"> 
</form> 
<&#63; 
echo $v; 
$_SESSION[status] = $v; 
&#63;> 
Copy after login
Copy after login

以上就是php防止CC攻击的多种方式,希望能帮助大家防止网页快速恶意刷新。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles