1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Redirect for certain IP range

Discussion in 'PHP & Perl' started by tas26, Dec 26, 2012.

  1. tas26

    tas26 Power Member

    Joined:
    Apr 21, 2009
    Messages:
    548
    Likes Received:
    274
    Occupation:
    student
    Location:
    BHW
    I have a redirect code added to my site. what i am looking for is a script which can check users IP and match it with given range if it meets the criteria then the redirect should not kick in else the redirect should work.

    Let me know if anyone can help me with this.
     
  2. johndoejohndoe

    johndoejohndoe Junior Member

    Joined:
    Feb 13, 2011
    Messages:
    177
    Likes Received:
    33
    I googled for you and came up with this:


    PHP:
    $range_start ip2long("68.61.156.0");
    $range_end   ip2long("68.61.181.255");
    $ip ip2long($_SERVER['REMOTE_ADDR']);
    if (
    $ip >= $range_start && $ip <= $range_end) {  // blocked}
     
    • Thanks Thanks x 4
  3. tas26

    tas26 Power Member

    Joined:
    Apr 21, 2009
    Messages:
    548
    Likes Received:
    274
    Occupation:
    student
    Location:
    BHW
    you rock. + Rep added.
     
  4. Crazy

    Crazy Jr. Executive VIP

    Joined:
    Jun 13, 2009
    Messages:
    640
    Likes Received:
    319
    Occupation:
    VB, C#, XHTML, CSS, PHP, MySQL, JavaScript, jQuery
    Location:
    Everywhere
    I would have suggested a regex pattern but this is much more elegant.
     
  5. Minarik1987

    Minarik1987 Junior Member

    Joined:
    Jul 18, 2012
    Messages:
    172
    Likes Received:
    26
    Great share I'm also needing this.