Okay so i have a page when it is visted stores there ip to file and bans there ip access to a differant page. Problem i have is it will only ban the last visted ip, which i want to ban all ip's in the file.

page1 which is visted and sotes ip in blocked_ips.php

<?
$ipaddress = "\r\n" . $_SERVER['REMOTE_ADDR'];

$file = 'blocked_ips.php';

$fp = fopen($file, 'a');

fwrite($fp, $ipaddress);

fclose($fp);
?> 

page2 which denys access to the ip

<?php
$deny_ips = file('ban/blocked_ips.php');

// read user ip address:
$ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : '';

// search current IP in $deny_ips array
if (($i = array_search($ip, $deny_ips)) !== FALSE){

// user is blocked:
print "<CENTER> YOU HAVE BEEN BANNED ! </CENTER>";
exit;
}

// If we reach this section, the IP address is valid

?>
<?php

so again the idea is to block/ban all ips in the blocked_ips.php, any help would be greatly appreciated.

thank you

Hi,

I think that your script works but you should do tests in different machines to know if every time the ip adress has been blocked or not.

thanks akmozo . well like i said it does log the ip but will not block all ips just the last one that was logged. i have done tests from differant pcs on this and it will not ban all the ips from the file.

any ideas?

Try with in_array.

thanks akmozo ... hmmmm... ill give it a shot .. not sure how todo that.

Hi again,

Try this :

<?php

    $ips_file_path = 'ips.php';   

    $my_ip = $_SERVER['REMOTE_ADDR'];

    ///////////////// use array /////////////////

    $ips_list = file($ips_file_path);

    foreach (array_values($ips_list) AS $ip){
        if (trim($ip) == $my_ip){
            print "<CENTER> YOU HAVE BEEN BANNED ! </CENTER>";
            exit;
        }
    } 
    echo 'Authorized ip !';        

    ///////////////// use string /////////////////

    $ips = file_get_contents($ips_file_path);

    if(strstr($ips, $my_ip)){
        print "<CENTER> YOU HAVE BEEN BANNED ! </CENTER>";    
    }else{
        echo 'Authorized ip !';
    }

?>

<?php
// * Auto IP Ban Script - v1.0 *
// * Author: Inque187 *
// * Date: (c) Oct 5, 2007 *
// * Works with PHP 4.0+ & 5.0+ *

// * DISCLAIMER: Author of this script is not and will not be held liable and or responsibile for any configuration errors, loss of data, interruption of *
// * service, and or any other means of misuse by neglect pertaining to this script that affects any internet account. User of this script takes sole *
// * responsibility for any resulting problems that arise due to usage of this script in part or in whole and shall hold indemnity against the author. *

$ipad = $_SERVER['REMOTE_ADDR'];    // Collects the user/visitor IP address.
$ban = "Deny from $ipad\n";         // What will be written to the .htaccess file if IP needs to be banned.
$file = "htaccess.txt";                 // Change to -> .htaccess <- once thoroughly tested. Should be placed in the root directory.
$search = file_get_contents($file);     // Prepare the .htaccess file by gathering entire contents.
$check = strpos($search, $ipad);        // Checks the .htaccess file if the current user IP address string does exist.

// This next part of the script checks to see if the IP is already banned or not.
// If the IP does not already exist; it will write the ban line to the .htaccess file, display the message, and then email you a copy.
// If the IP is already listed in the .htaccess file; the script ends with only a displayed message.
if ($check === FALSE) {

$open = @fopen($file, "a");         // Open the .htaccess file and get ready for writing only.
$write = @fputs($open, $ban);       // Write the banned IP line to the .htaccess file. (Example: Deny from 12.34.56.789)

// Email a copy of ban and info to your admin account (or other email address).
// Make sure you change the email address.
@mail('BlOcK_IpS@YoUr_WeB_sItE.cOm','Banned IP '.$_SERVER['REMOTE_ADDR'].'','
Banned IP: '.$_SERVER['REMOTE_ADDR'].'
Request URI: '.$_SERVER['REQUEST_URI'].'
User Agent: '.$_SERVER['HTTP_USER_AGENT']);

// IP address is not banned - so there is a need to write to .htaccess file.
// Display the error message to the user. (You may change to read what you want).
    echo '<html><head><title>IP Address '.$ipad.' - Blocked or Banned!</title></head><body bgcolor="#FF000000" text="#FFFFFF" oncontextmenu="return false;"><center><font face="Verdana, Arial"><h1>THANK YOU - DON\'T COME AGAIN!</h1><b>IP Address '.$ipad.' Has Been Blocked or Banned!<br />Contact the web admin if this ban is by mistake.<p />Have a nice day!</b></font></center></body></html>';

// Close the .htaccess file - all done.
@fclose($open);
} else {

// IP address is already banned - no need to rewrite to .htaccess file.
// Display the error message to the user. (You may change to read what you want).
    echo '<html><head><title>IP Address '.$ipad.' - Blocked or Banned!</title></head><body bgcolor="#FF000000" text="#FFFFFF" oncontextmenu="return false;"><center><font face="Verdana, Arial"><h1>THANK YOU - DON\'T COME AGAIN!</h1><b>IP Address '.$ipad.' Has Been Blocked or Banned!<br />Contact the web admin if this ban is by mistake.<p />Have a nice day!</b></font></center></body></html>';
}

// End of File/Script;
exit;
?>

You can try this code for band all ip adress.

if (($i = array_search($ip, $deny_ips)) !== FALSE){
// user is blocked:
print "<CENTER> YOU HAVE BEEN BANNED ! </CENTER>";
exit;
}

Not sure if that will work, makes more sense to me like this:

if (array_search($ip, $deny_ips) !== FALSE){
// user is blocked:
print "<CENTER> YOU HAVE BEEN BANNED ! </CENTER>";
exit;
}

I would database it for better control, you could ban for a set time etc. Likely faster once the file starts to get big as well

Also check the data in the array $deny_ips: http://uk3.php.net/manual/en/function.file.php

according the that file() will include the line breaks into the value so that "12.34.56.78" != "12.34.56.78\r\n";

try file('ban/blocked_ips.php',FILE_IGNORE_NEW_LINES);

thanks for the replies . will Try them out when I get home.

Thanks so much guys they all work!!

now just testing them all to see if theres any bugs.

again THANK YOU for the fast and reliable help.

hello again,
these scripts worked perfectly .
im back to these codes beacuse im tring to add a time limit per ip ban.
i have tried numerous scripts without any luck. any ideas how be great thanks