Solved

Ban IP's

Posted on 2004-10-30
256 Views
Last Modified: 2012-05-05
Hi all,

I had a previous question here before on banning certain IPs and redirect them to another page

I got the following code


<?php
/* put this at the top of your page */
$certain_ips = array('62.215','62.150','195.226','195.39','213.189','213.181','168.187','194.54');
foreach($certain_ips as $ip)
{
 if(substr($_SERVER['REMOTE_ADDR'], 0, strlen($ip)) == $ip)
 {
  header('Location:login.php');
  exit();
 }
else
{
header('Location:chat.php');
exit();
}
}
?>


The IPs shown are the banned IPs in my site. The problem with this code that it bans only the first two IPs but the rest still can access the site normally.

Anyone can fix that plz?

0
Question by:Souled
    5 Comments
     
    LVL 27

    Accepted Solution

    by:
    $certain_ips[8]

    should be

    $certain_ips

    and by the looks of it you should cut out the else clause, ie:

    /* put this at the top of your page */
    $certain_ips = array('62.215','62.150','195.226','195.39','213.189','213.181','168.187','194.54');
    foreach($certain_ips as $ip)
    {
     if(substr($_SERVER['REMOTE_ADDR'], 0, strlen($ip)) == $ip)
     {
      header('Location:login.php');
      exit();
     }
    }

    header('Location:chat.php');
    exit();

    }

    otherwise for everyone of the ip's its going to redirect to one or the other pages, if the ip doesnt match then it will automatically load the other page. With the above it will check all of the ips then if none of them are banned it will redirect to chat.php, not sure if thats the expected behaviour but thats how i read into it.

    failing that did you try the alternative code i posted in your previous question?
    0
     
    LVL 27

    Expert Comment

    by:Diablo84
    sorry that last } should not be there
    0
     

    Author Comment

    by:Souled

    I noticed that.
    It gave me error when I processed it.

    Now its all fine.

    Thanks man

    0
     
    LVL 27

    Expert Comment

    by:Diablo84
    no problem :)
    0
     

    Author Comment

    by:Souled
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    Suggested Solutions

    As you all know PHP got one problem, before using other files classes or functions you need to include or use require function. But now in PHP 5 there's solution for this, it's called __autoload() function. You can read more about function in PHP ma…
    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now