


PHP prevents CC attack code PHP prevents frequent refresh of web pages, _PHP tutorial
php prevents CC attack code php prevents frequent refresh of web pages,
rapid and malicious refresh of web pages, CC attack is when the attacker uses a proxy server to generate legitimate requests directed to the target site, simulating multiple users Continuously accessing the victim website, especially those pages that require a large amount of data operations and require a lot of CPU time, will eventually lead to the target website server resources being exhausted, until it crashes. This will cause a waste of server resources and CPU If it stays at 100% for a long time, there will always be connections that cannot be processed until the network is congested and normal access is suspended. We call it a CC attack. Although we can use some anti-attack software to achieve this, the effect is sometimes not obvious.
Below I provide a piece of PHP code that can prevent CC attacks to a certain extent.
Main functions: Continuously refreshing the page more than 5 times within 3 seconds will point to this machine http://127.0.0.1
Just put this code on the page that needs to prevent CC attacks.
Source code
<?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; } ?>
The following is an effective method for preventing CC attacks in PHP:
1.session record
submit.php is the sending page. Set a session variable on this page and send it to the submitdeal.php page as a hidden field and form. On the server side, compare the hidden variable posted with the session variable recorded on the server side, for example If the two values are the same, write to the database and clear the session. If the user refreshes the page, an error or jump will be prompted if the two values are not equal.
Advantages: No need for user to enter verification code
Disadvantages: The form is easily copied
2.Verification code
The principle is the same as the first one, except that the session data is not submitted as a hidden field, but is asked to be filled in by the user. Most websites use text verification codes and picture verification codes, and picture verification codes are highly secure.
3.IP binding
After submitting the data, first retrieve the client IP from the IP table. If it exists and has not expired, an error will be reported. Otherwise, it will be written to the database. Then the guest room IP will be retrieved and the IP will be written to the database.
4.cookie:After the client submits, the processing program first searches whether the client has set a cookie. If so, it will not submit again; if not, it will write the data and then write a cookie;
Example:
Users' meaningless frequent jumps and requests will put a lot of burden on the server. In fact, using cookies can prevent this.
<?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!"; ?>
Method 1:
<?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; } ?>
Method 2:
<? 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; ?>
Method 3:
<? 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; ?>
The above are various ways for PHP to prevent CC attacks. I hope it can help everyone prevent quick and malicious refreshes of web pages.
Articles you may be interested in:
- Let PHP COOKIE take effect immediately and use it without refreshing
- Summary of the code in php to prevent malicious page refreshes
- PHP methods to prevent malicious refreshes and ticket fraud
- A summary of PHP methods to prevent the website from being refreshed
- Refresh the PHP buffer to speed up your site
- PHP prevents refreshing of repeatedly submitted pages The sample code
- php uses session to prevent repeated page refresh

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools