Arkadaşlar sahip olduğum sitemdeki arama kısmına ne olur ne olmaz die, flood koruması koymak istedim. Ziyaretçi min 30sn de bir arama yapabilsin istiyorum.
Extra olarak hangi kodu modifiye etmem gerekir? Ayrıca 30sn den önce arama yaparsa uyarıda çıkarsa fena olmaz ama zorunlu deil.
Flood için koruma
5
●2.368
- 13-01-2007, 13:19:49Merhaba;
Sana Murat Atay'ın hazırladığı class ı gönderiyorum.. Gayet basit gibi görünmesine rağmen çok kullanışlı..
<?php class flooddam { /* FloodDam 1.0.4 Copyright (C) 2005 Murat ATAY This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not,write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ // [TR] IP adresi bulunamayan kullanıcılara izin ver. // [EN] Allow users when IP not found. var $allow_noip = FALSE; // (TRUE / FALSE) // [TR] Okuma veya yazma hatası durumunda izin ver. // [EN] Allow when read or write error. var $allow_error = FALSE; // (TRUE / FALSE) // [TR] IP listesinin tutulacağı dizin adı. Yazılabilir olmalıdır. // [EN] IP list log directory. Must be writeable. var $directory = 'ip'; // [TR] Maksimum istek sayısı. // [EN] Maximum request number. var $hits = 10; // [TR] Maksimum istek sayısı için minimum süre. // [EN] Minimum time for the maximum request number. var $seconds = 5; // [TR] $ban_time süre sonra blokeleri kaldır. // [EN] Remove bans after $ban_time later. var $clear_bans = TRUE; // (TRUE / FALSE) // [TR] IP nin ne kadar süre bloke edileceğini belirten süre. // [EN] Time for how much time the IP will be banned. var $ban_time = "+1 day"; // [TR] ÖRNEK: $seconds saniye içerisinde $hits adet istek yapılırsa IP bloke edilir. // [EN] EXAMPLE: If requests are more than $hits within $seconds seconds than IP will be blocked. // [TR] Ne yaptığınızı gerçekten bilmiyorsanız aşağıdaki satırları değiştirmeyin. // [EN] Don't change lines below unless you really know what you are doing. var $blocked = FALSE; var $client_ip = NULL; var $client_seconds = 0; var $client_average = 0; function flooddam() { if (!$this->client_ip = $this->find_client_ip()) { if (!$this->$allow_noip) { exit(); } } else { if ($this->file_ip_banned($this->client_ip)) { exit(); } if ($this->file_ip_check($this->client_ip)) { $time_array = explode(';', $this->file_ip_read($this->client_ip)); } $time_array[] = time(); if (count($time_array) >= $this->hits) { if (($this->client_seconds = $time_array[count($time_array) - 1] - $time_array[count($time_array) - $this->hits]) < $this->seconds) { $this->blocked = TRUE; } $time_array = array_slice($time_array, -$this->hits); } $this->client_average = (int) ($time_array[count($time_array) - 1] - (array_sum($time_array) / count($time_array))); $this->times = implode(';', $time_array); if (!$this->file_ip_write($this->client_ip, $this->times)) { if ($this->allow_error) return; exit(); } if ($this->blocked) { if ($this->clear_bans) { $this->file_clean_ip_ban(); } $this->file_ip_ban($this->client_ip, time()); exit; } } } function file_ip_ban($ip, $text) { $fp = fopen($this->directory . '/' . $ip . '.banned', 'wt'); fwrite($fp, $text); fclose($fp); } function file_clean_ip_ban() { if ($dir = opendir($this->directory)) { while (($file = readdir($dir)) !== FALSE) { $fileinfo = pathinfo($file); if ($fileinfo['extension'] == 'banned') { if (time() > strtotime($this->ban_time, $this->file_read($fileinfo['basename']))) { unlink($this->directory . '/' . $fileinfo['basename']); } } } } closedir($dir); } function file_ip_read($ip) { $return = file_get_contents($this->directory . '/' . $ip . '.txt'); return $return; } function file_read($file) { $return = file_get_contents($this->directory . '/' . $file); return $return; } function file_ip_write($ip, $text) { if ($fp = @fopen($this->directory . '/' . $ip . '.txt', 'wt')) { fwrite($fp, $text); fclose($fp); return TRUE; } else {return FALSE;} } function file_ip_check($ip) { return is_file($this->directory . '/' . $ip . '.txt'); } function file_ip_banned($ip) { return is_file($this->directory . '/' . $ip . '.banned'); } function find_client_ip() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) {return $_SERVER['HTTP_CLIENT_IP'];} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {return $_SERVER['HTTP_X_FORWARDED_FOR'];} elseif (!empty($_SERVER['REMOTE_ADDR'])) {return $_SERVER['REMOTE_ADDR'];} else {return NULL;} } } ?>Kullanımı da gayet basit;
<?php require('flooddam.php'); ?> <?php $flooddam = new flooddam(); ?> FloodDam Demo<br> <br> Bu demoya bağlandığınız IP adresiniz <b><?=$flooddam->client_ip;?></b> dir.<br> <br> <?php if (empty($flooddam->client_seconds)) { ?> Henüz maksimum istek sayısı <b><?=$flooddam->hits;?></b>'e ulaşmadınız. <?php } else { ?> Son yaptığınız <b><?=$flooddam->hits;?></b> istek arası geçen süre <b><?=$flooddam->client_seconds;?></b> saniyedir.<br> Son yaptığınız <b><?=$flooddam->hits;?></b> istek arası geçen ortalama süre <b><?=$flooddam->client_average;?></b> saniyedir.<br> <br> Minimum süre olan <b><?=$flooddam->seconds;?></b> saniyeyi aşarsanız IP adresiniz bloke edilecektir. <?php } ?> - 06-05-2009, 17:14:23meraba,
ben de böyle bir sistem arıyordum. bu kodu sayfamızın neresinde kullanmamız gerekir, yardımcı olursanız çok sevinirim.. - 08-05-2009, 18:05:44Bunun için bu kadar kastıracak bir kod koymaya gerek var mı? Ben olsam bunu session ile hallederdim. şöyleki.
Arama yap dediğin zaman $_SESSION[aramazamani] = now() derim. tekrar arama yaptığında arama zamanı ise sessiondaki değer ile farkını alacaksınız.
misal
if(isset($_SESSION["aramazamani"])) { if(abs(now() - $_SESSION["aramazamani"])>30) $_SESSION["aramazamani"] = now(); $aramayapsin = true; else $aramayapsin = false; } else { $_SESSION["aramazamani"] = now(); $aramayapsin = true; }dersin arama yapması için de
if($aramayapsin){ //Arama yapmasına izin ver }else{ echo 'Arama yapabilmeniz için '.$_SESSION["aramazamani"]-now()+30' saniye beklemelisiniz'; }tarzi birşey ile kastırmadan yapabilirsiniz.
Kolay gelsin.