Как с помощью iptables, но менее безопасно, но эффективно работает.
<?php
/*
@author InCube
@copyright 2013 Gnu gpl 3.0
@des Protect from spamming.
@usage create dir called iplog chmod 7777 and just put this code <?php require "PATH/TO/THIS/FILE/anti_dos.php";?> in main file
*/
class AntiIncubeDos
{
public $cookie;
public $othercookie;
public $iptime;
public $ippenalty = 600;
public $ipmaxvisit;
public $iplogdir = "./iplog/";
public $iplogfile = "iplog.dat";
public $ipfile;
public $oldtime;
public $time;
public $newtime;
public $oldref;
public $domain;
public function init()
{
$this->time = time();
$this->ipfile = substr(md5($_SERVER["REMOTE_ADDR"]), -2);
$this->cookie = $_COOKIE['RaiNran'];
$this->othercookie = $_COOKIE['RaiNRanOnline'];
$this->iptime = 10;
$this->ipmaxvisit = 10;
$this->oldtime = file_exists($this->iplogdir . $this->ipfile) ? filemtime($this->iplogdir . $this->ipfile) : 0;
$this->oldtime = $this->oldtime < $this->time ? $this->time : $this->oldtime;
$this->newtime = $this->oldtime + $this->iptime;
$this->block();
touch($this->iplogdir . $this->ipfile, $this->newtime);
}
public function block()
{
if ($this->newtime >= $this->time + $this->iptime * $this->ipmaxvisit) {
touch($this->iplogdir . $this->ipfile, $this->time + $this->iptime * ($this->ipmaxvisit - 1) + $this->ippenalty);
$this->oldref = $_SERVER['HTTP_REFERER'];
header("HTTP/1.0 503 Service Temporarily Unavailable");
header("Connection: close");
header("Content-Type: text/html");
?>
<html>
<head>
<title>503 Service temporary unavailable</title>
<style>
body
{
background-color:black;
color:white;
}
</style>
</head>
<body>
<h1>Temporary Access Denial</h1>
Too many quick page views from your IP address (You got more than <?= $this->ipmaxvisit ?> visits in <?= $this->iptime ?> seconds).<br/>
Ban will be lifted in: <?= $this->ippenalty ?> secs.
</body>
</html>
<?php
touch($this->iplogdir . $this->iplogfile);
$fp = fopen($this->iplogdir . $this->iplogfile, "a");
$this->domain = $_SERVER['HTTP_HOST'];
if ($fp) {
$this->useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "(User Agent: UNKNOWN)";
fwrite($fp, $_SERVER['REMOTE_ADDR'] . " " . date("Y-m-d H:i:s") . " " . $this->useragent);
// Continue code if ONLY its writtable
fclose($fp);
if ($_SESSION['reportedflood'] < 1 && ($this->newtime < $this->time + $this->iptime + $this->iptime * $this->ipmaxvisit)) {
// Do whatever you want here. Send mail or wtf i dunno.
}
}
touch($this->iplogdir . $this->ipfile, $this->newtime);
}
}
}
$dos = new AntiIncubeDos;
$dos->init();
?>