Redirect By IP

Discussion in 'PHP' started by kazanova_ks, Jun 1, 2007.

  1. #1
    som1 have PHP script or java or can tell me how i can redirect all my visitors with the ip address that starts with 10.0. will be directed to Site A, otherwise they get directed to Site B


    or some1 can modify this in PHP cuse this is ASP :confused:


    <?
    remoteIP = request.servervariables("REMOTE_ADDR")
    if left(remoteIP,5)="10.0." then page="Site A" else page="Site B"
    Response.Status = "301 Moved Permanently"
    Response.AddHeader "Location", page
    ?>
     
    kazanova_ks, Jun 1, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    if (substr($_SERVER['REMOTE_ADDR'], 0, 5) == '10.0.')
        $page = 'pageA';
    else
        $page = 'pageB';
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: gtfo.php');
    PHP:
    Though I would only use the 301 redirect when the IP condition matches. But then again, I don't know your exact situation.
     
    krt, Jun 1, 2007 IP
  3. kazanova_ks

    kazanova_ks Active Member

    Messages:
    99
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #3
    thnx man but its dosent work :( som1 help me pls

    i fund and this but i cant modify cuse i dont have knowlege in php

    <?php
    if ( getenv("REMOTE_ADDR") != "10.0.0.1" ) {
    header("Location: http://otherhosthere");
    }
    ?>
     
    kazanova_ks, Jun 1, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    I decided to test it on my local server and online and it works in both cases. Can I ask what was the problem with my code? Was there an error? Did it display a blank page? etc.
     
    krt, Jun 1, 2007 IP
  5. kazanova_ks

    kazanova_ks Active Member

    Messages:
    99
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #5
    thnx man with your it was blank page and not redirectet me ... i find script thnx man from all thnx the working script is this

    <?
    $visitor = $_SERVER['REMOTE_ADDR'];
    if (preg_match("/200.xxx.xxx.xxx/",$visitor)) {
    header('Location: http://google.com');
    } else {
    header('Location: http://yahoo.com');
    };
    ?>

    or download here
     
    kazanova_ks, Jun 1, 2007 IP
  6. kazanova_ks

    kazanova_ks Active Member

    Messages:
    99
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #6
    but i have another question how can i put more IP adreses here is just 1 but i need more IP


    <?
    $visitor = $_SERVER['REMOTE_ADDR'];
    if (preg_match("/200.xxx.xxx.xxx/",$visitor)) {
    header('Location: http://google.com');
    } else {
    header('Location: http://yahoo.com');
    };
    ?>
     
    kazanova_ks, Jun 1, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Hmmm... that looks exactly like my code without the 301 header. Oh well.

    200.xxx.xxx.xxx won't match any IP, "x" in the regex means a literal "x" and a "." represents any character, not a literal "dot".
    Refer to: _http://www.regular-expressions.info/reference.html

    Here is the proper regex to match an IP starting with 200.
    ^200
    or:
    200\.\d{1,3}\.\d{1,3}\.\d{1,3}

    If you want to match more IPs, just post the list here as it is hard for someone new to regex to be able to do this.

    If you want to try, use this template:
    \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
    Code (markup):
    Each \. represents a dot and the \d{1,3} represents a 1-3 digit number.

    Replace \d{1,3} with the number you want to match and pipe these together.

    Eg: to block 200.*.*.* and 10.0.*.*

    Use:
    (200\.\d{1,3}\.\d{1,3}\.\d{1,3}|10\.0\.\d{1,3}\.\d{1,3})
    
               note the pipe (|)  /\
    Code (markup):
    A shorter way is:
    ^(200|10\.0)\.
    Code (markup):
     
    krt, Jun 2, 2007 IP
    pig2cat likes this.
  8. kazanova_ks

    kazanova_ks Active Member

    Messages:
    99
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    93
    #8
    thnx man for your work to many times thnx

    this is very hard to me cuse i am not a good in php i have 1 question is ther any way just to write ip for exampel i just put that X to hide ip in past post ,but her in my php script its work good like this 213.163.105.50 not decodet (how you explain me but real like 213.163.105.50 )or how to say soory for my bad english man so im trying to do all of myself to be understandet i dont know if i gat that but maybe you understand what im tryning to say


    <?
    $visitor = $_SERVER['REMOTE_ADDR'];
    if (preg_match("/213.163.105.50/",$visitor)) {
    header('Location: http://google.com');
    } else {
    header('Location: http://yahoo.com');
    };
    ?>


    if i like to put and another ip is there any way to put mothing like this exempl second ip 189.234.345.543 and to put like in script down cuse you are right but its very hard for me you help me today but i havent you in future there is a problem thn man a lot


    <?
    $visitor = $_SERVER['REMOTE_ADDR'];
    if (preg_match("/213.163.105.50;189.234.345.543/",$visitor)) {
    header('Location: http://google.com');
    } else {
    header('Location: http://yahoo.com');
    };
    ?>


    soory for bad english im trying to do best of me thnx man for helping 10000 thnx
     
    kazanova_ks, Jun 2, 2007 IP
  9. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #9
    Instead of:
    if (preg_match("/213.163.105.50;189.234.345.543/",$visitor)) {
    Code (markup):
    Use:
    if (preg_match("/(213\.163\.105\.50|189\.234\.345\.543)/",$visitor)) {
    Code (markup):
    Note the \. is needed to match a "literal dot", it will work with a normal "." but not for the reason you may think.

    Also, instead of a colon to make a list of candidates, use a pipe (|). One last thing, when you use a pipe to make a list, the list must be enclosed in parenthesis "( and )".

    Of course, if you want to blacklist full IPs, you do not need regex.

    Just use:
    if (in_array($visitor, array('213.163.105.50', '189.234.345.543'))) {
    Code (markup):
     
    krt, Jun 2, 2007 IP