php防止CC攻击代码 php防止网页频繁刷新,
php防止CC攻击代码 php防止网页频繁刷新,
网页快速恶意刷新,cc攻击就是攻击者利用代理服务器生成指向目标站点的合法请求,模拟多用户不停的对受害网站进行访问,特别是访问那些需要大量数据操作需要大量CUP时间的页面,最终导致目标网站服务器资源耗尽,一直到宕机崩溃,如此一来,造成服务器资源的浪费,CPU长时间处于100%,永远都有处理不完的连接直至就网络拥塞,正常的访问被中止。我们称之为CC攻击。尽管我们可以借助于一些防攻击的软件来实现,不过效果有时并不明显。
下面我提供一段PHP的代码,可以起到一定的防CC攻击效果。
主要功能:在3秒内连续刷新页面5次以上将指向本机 http://127.0.0.1
只需将该代码放到需要防CC攻击的页面即可。
源码
<?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; } ?>
下面是为大家整理的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就可以防止这一点。
<?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!"; ?>
方式一:
<?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; } ?>
方式二:
<? 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); ?> <form method="post" name="magic" action="f5.php"><input type="hidden" name="tag" value="<?=$v?>"><input type=text name="name"><input type="submit" value="submit"> </form> <? echo $v; $_SESSION[status] = $v; ?>
方式三:
<? 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); ?> <form method="post" name="magic" action="f5.php"><input type="hidden" name="tag" value="<?=$v?>"><input type=text name="name"><input type="submit" value="submit"> </form> <? echo $v; $_SESSION[status] = $v; ?>
以上就是php防止CC攻击的多种方式,希望能帮助大家防止网页快速恶意刷新。
您可能感兴趣的文章:
- 让PHP COOKIE立即生效,不用刷新就可以使用
- php中防止恶意刷新页面的代码小结
- php防止恶意刷新与刷票的方法
- php防止网站被刷新的方法汇总
- 刷新PHP缓冲区为你的站点加速
- PHP防止刷新重复提交页面的示例代码
- php采用session实现防止页面重复刷新

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
