Solved

Redirect certain IP's

Posted on 2004-10-29
271 Views
Last Modified: 2008-02-26
Hi all,

I need to redirect a certain IP's to a specific login page.
for example:
IP: 62.150.X.X
redirected to login.php

The reason I need this code is that I would like to redirect a certain country to a login page on my chat site, other countries have direct access to the chat service without providing username/password.

One more thing, I have around 10 IP's to redirect.


0
Question by:Souled
    8 Comments
     
    LVL 49

    Expert Comment

    by:Roonaan
    Try the snippet below:

    <?php
    /* put this at the top of your page */
    $certain_ips = array('62.150.0.1','62.150.0.2');
    if(in_array($_SERVER['REMOTE_ADDR'], $certain_ips))
    {
    header('Location:login.php');
    exit();
    }
    ?>

    Regards

    -r-
    0
     

    Author Comment

    by:Souled

    I have tried it and it worked fine.

    one thing has to be changed though. The form which the IPs r entered here is static.
    What i meant 62.150.X.X is that we need to use only 62.150 and any other possible class following without entering them manually.

    I have tried to change this line to the following form

    $certain_ips = array('62.150','213.189');

    but it did not work.

    any solution for that?

    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    Well, you could use:

    foreach($certain_ips as $ip)
    {
     if(substr($_SERVER['REMOTE_ADDR'], 0, strlen($ip)) == $ip)
     {
      header('Location:login.php');
      exit();
     }
    }

    An alternative would be a preg match, but the solution above is somewhat more maintainable in my opinion.

    -r-
    0
     

    Author Comment

    by:Souled
    How can I specify the banned IPs in the last code?

    0
     
    LVL 49

    Accepted Solution

    by:
    The array $certain_ips contain the leftmost parts of the addresses that need to be redirected.

    The array
    $certain_ips = array('62.150','213.189');
    Would ban
    62.150.*.* and 213.189.*.*

    -r-

    0
     
    LVL 27

    Expert Comment

    by:Diablo84
    A regex example derived from an IP blocking script I had in action on a site i built some time back:

    $ip[] = '^([6][2](\.[1][5][0])(\.[0-9]{1,3}){2})$';

    //some other examples of use, you can remove this block of code if you don't need it for reference
    //$ip[] = '111.111.111.111';
    //$ip[] = '^([1][1][1](\.[0-9]{1,3}){3})$';  //BAN 111.*
    //$ip[] = '^([1][1][1](\.[1][1][1])(\.[0-9]{1,3}){2})$';  //BAN111.111.*
    //$ip[] = '^([1][1][1](\.[1][1][1])(\.[1][1][1])(\.[0-9]{1,3}))$';  //BAN 111.111.111.*
    //$ip[] = '^([0-9]{1,3}(\.[0-9]{1,3}){3})$';  //to ban ALL

    $user_ip = $_SERVER['REMOTE_ADDR'];

    foreach($ip as $var) {
     if(ereg($var,$user_ip)){
      header("location: login.php");
      exit;
     }
    }
    0
     

    Author Comment

    by:Souled


    it worked just fine THANKS ALOT MAN


    u earned it :)


    0
     

    Author Comment

    by:Souled
    Roonaan

    I know had accepted the code already
    but I have a small problem with it now

    I have a list of 10 banned IPs. The code provided by you bans only the first two in the array.

    Could you fix that? I can put this question as new ticket if you wish
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    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 …

    857 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

    18 Experts available now in Live!

    Get 1:1 Help Now