IP Redirect

Discussion in 'Programming' started by deka, Nov 1, 2008.

  1. #1
    anyone can help me fix this code?.. its a ip redirect code based on ip.txt list..

    i want ip that is in the ip.txt will be redirected to the fake site..

    deny.php
    
    <?php
    $iplist = explode("\n",file_get_contents('ip.txt'));
    $ip = $_SERVER['REMOTE_ADDR'];
    $real_site='http://www.realpage.com';
    $fake_site='http://www.fakesite.com';
    if ($ip != $iplist)
    {
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location:$fake_site"); 
    }
    else
    {
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location:$real_site");
    }
    ?>
    
    Code (markup):
    thanks..
     
    deka, Nov 1, 2008 IP
  2. penalty

    penalty Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    hi, explode() returns you an array.
    try this:
    if(in_array($ip, $iplist)) { // this is true, if the ip is in the iplist, 
    ...
    } else {
    ...
    }
    Code (markup):
     
    penalty, Nov 1, 2008 IP
  3. deka

    deka Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    $iplist = explode("\n",file_get_contents('ip.txt'));
    $ip = $_SERVER['REMOTE_ADDR'];
    $real_site='http://www.realpage.com';
    $fake_site='http://www.fakesite.com';
    if(in_array($ip, $iplist)) 
    {
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location:$real_site");
    } 
    else 
    {
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location:$fake_site"); 
    }
    ?>
    
    Code (markup):
    using this code, i keeps going to fakesite url.. whether i include my ip or not in ip.txt
     
    deka, Nov 1, 2008 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    
    <?php
    $iplist = file_get_contents('ip.txt');
    $ip = $_SERVER['REMOTE_ADDR'];
    $real_site='http://www.realpage.com';
    $fake_site='http://www.fakesite.com';
    if(ereg($ip,$iplist))
    {
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location:$fake_site");
    } 
    else 
    {
    Header("HTTP/1.1 301 Moved Permanently");
    Header("Location:$real_site"); 
    }
    ?>
    
    PHP:
    You got them switched. No need to explode or create an array.

    Peace,
     
    Barti1987, Nov 1, 2008 IP
  5. deka

    deka Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Woohoo.. its working now..thanks azizny.. btw, when i try this script on my other hosting, it works couple time, but suddenly it shows 404 from that hosting, what can u think off that server done?
     
    deka, Nov 1, 2008 IP
  6. deka

    deka Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Paul, btw.. what is the different within your script n this:

    my friend just give me this script
    
    <?php
    
    $ip            = trim($_SERVER['REMOTE_ADDR']);
    $iplist        = array_map('trim', explode("\n", file_get_contents('ip.txt')));
    $fake_site     = 'http://www.fakesite.com';
    $real_site     = 'http://www.realsite.com';
    
    if(in_array($ip, $iplist))
    {
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: '.$fake_site); 
    }
    else
    {
        header('HTTP/1.1 301 Moved Permanently');
        header('Location: '.$real_site); 
    }
    
    ?> 
    
    Code (markup):
    which is the most lite using system resources?..
     
    deka, Nov 1, 2008 IP