Home php教程 php手册 php防止网站被攻击办法

php防止网站被攻击办法

May 25, 2016 pm 04:40 PM

最近网站经常被攻击,后来想到了一个利用php来防止网站受攻击的办法,下面是我的代码,代码不是最好的,根据自己的需求来做,下面来看看我的代码.

<?php
/*
 *网站防ip攻击代码(anti-ip attack code website)2010-11-20,ver2.0
 *mydalle.com anti-refresh mechanism
 *design by www.phprm.com
*/
//查询禁止ip
$ip = $_server[&#39;remote_addr&#39;];
$fileht = ".htaccess2";
if (!file_exists($fileht)) file_put_contents($fileht, "");
$filehtarr = @file($fileht);
if (in_array($ip . "rn", $filehtarr)) die("warning:" . "
" . "your ip address are forbided by mydalle.com anti-refresh mechanism, if you have any question pls emill to shop@mydalle.com!
(mydalle.com anti-refresh mechanism is to enable users to have a good shipping services, but there maybe some inevitable network problems in your ip address, so that you can mail to us to solve.)");
//加入禁止ip
$time = time();
$fileforbid = "log/forbidchk.dat";
if (file_exists($fileforbid)) {
    if ($time - filemtime($fileforbid) > 30) unlink($fileforbid);
    else {
        $fileforbidarr = @file($fileforbid);
        if ($ip == substr($fileforbidarr[0], 0, strlen($ip))) {
            if ($time - substr($fileforbidarr[1], 0, strlen($time)) > 120) unlink($fileforbid);
            elseif ($fileforbidarr[2] > 120) {
                file_put_contents($fileht, $ip . "rn", file_append);
                unlink($fileforbid);
            } else {
                $fileforbidarr[2]++;
                file_put_contents($fileforbid, $fileforbidarr);
            }
        }
    }
}
//防刷新
$str = "";
$file = "log/ipdate.dat";
if (!file_exists("log") && !is_dir("log")) mkdir("log", 0777);
if (!file_exists($file)) file_put_contents($file, "");
$allowtime = 60; //防刷新时间
$allownum = 5; //防刷新次数
$uri = $_server[&#39;request_uri&#39;];
$checkip = md5($ip);
$checkuri = md5($uri);
$yesno = true;
$ipdate = @file($file);
foreach ($ipdate as $k => $v) {
    $iptem = substr($v, 0, 32);
    $uritem = substr($v, 32, 32);
    $timetem = substr($v, 64, 10);
    $numtem = substr($v, 74);
    if ($time - $timetem < $allowtime) {
        if ($iptem != $checkip) $str.= $v;
        else {
            $yesno = false;
            if ($uritem != $checkuri) $str.= $iptem . $checkuri . $time . "1rn";
            elseif ($numtem < $allownum) $str.= $iptem . $uritem . $timetem . ($numtem + 1) . "rn";
            else {
                if (!file_exists($fileforbid)) {
                    $addforbidarr = array(
                        $ip . "rn",
                        time() . "rn",
                        1
                    );
                    file_put_contents($fileforbid, $addforbidarr);
                }
                file_put_contents("log/forbided_ip.log", $ip . "--" . date("y-m-d h:i:s", time()) . "--" . $uri . "rn", file_append);
                $timepass = $timetem + $allowtime - $time;
                die("warning:" . "
" . "pls don&#39;t refresh too frequently, and wait for " . $timepass . " seconds to continue, if not your ip address will be forbided automatic by mydalle.com anti-refresh mechanism!
(mydalle.com anti-refresh mechanism is to enable users to have a good shipping services, but there maybe some inevitable network problems in your ip address, so that you can mail to us to solve.)");
            }
        }
    }
}
if ($yesno) $str.= $checkip . $checkuri . $time . "1rn";
file_put_contents($file, $str);
?>
Copy after login


文章链接:

随便收藏,请保留本文地址!

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)