PHP IP Ban/Redirect. Can you help with my script?

Discussion in 'PHP' started by X_X_ROB_X_X, Feb 24, 2008.

Thread Status:
Not open for further replies.
  1. #1
    Im in need of a little help with my php script if anyone has a bit of time. Simply put what i'm attempting to do is have the php script look at every person's IP as they come to my page. If their IP is in the banned_ip list i'm wanting to redirect that person to the first URL ( http://www.yahoo.com ).
    If their IP is not in the list I want to redirect these people to the second URL in the script ( http://www.google.com ).

    It's basically a php IP ban/redirect script. I have tried it on my host the way it is written and added my own IP to the banned list and it kept sending me to the 2nd URL when it should have sent me to the 1st. I'm lost. I'm a complete PHP newb guys so please keep the laughing and rude comments to a minimum, lol.

    Anyways i'd appreciate any and all help I can get. Below is my code.


    
    <?php 
    $banned_ip = array(); 
    $banned_ip[] = '204.96.148.3'; 
    $banned_ip[] = '205.162.217.141'; 
    $banned_ip[] = '209.62.82.14'; 
    $banned_ip[] = '24.7.101.103'; 
    $banned_ip[] = '65.31.130.2'; 
    $banned_ip[] = '68.168.78.226'; 
    $banned_ip[] = '68.205.75.110'; 
    $banned_ip[] = '69.36.158.35'; 
    $banned_ip[] = '70.173.198.69'; 
    $banned_ip[] = '70.97.122.18'; 
    $banned_ip[] = '71.189.157.53'; 
    $banned_ip[] = '71.230.54.244'; 
    $banned_ip[] = '74.52.245.146'; 
    $banned_ip[] = '76.90.3.205'; 
    $banned_ip[] = '24.20.251.76'; 
    $banned_ip[] = '68.83.254.243'; 
    $banned_ip[] = '69.2.27.10'; 
    
    foreach($banned_ip as $banned) {  
        $ip = $_SERVER['REMOTE_ADDR']; 
        if($ip == $banned){  
            header("location: http://www.yahoo.com"); 
    }
    else
    {
            header("location: http://www.google.com"); 
            exit();  
        }  
    }  
    
    
    ?>  
    
    Code (markup):
     
    X_X_ROB_X_X, Feb 24, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    echo your IP in the PHP script and make sure you're using the right IP.

    Furthermore, I'd do like like this:
    
    
    if (in_array($_SERVER['REMOTE_ADDR'], $banned_ip))
    {
        header('Location: http://yahoo.com');
        exit(); // This exit() is more important than the other
    }
    else
    {
        header('Location: http://google.com');
        exit();
    }
    
    PHP:
    It's way faster than the loop...
     
    nico_swd, Feb 24, 2008 IP
  3. X_X_ROB_X_X

    X_X_ROB_X_X Banned

    Messages:
    62
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you very much.
    Any ideas about banninjg a range of IPs if I want to use th same script as well. Like if i wanted to ban 208.80.193.[1 - 255]
     
    X_X_ROB_X_X, Feb 24, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    
    $banned_ip[] = '204.96.*.4'; 
    $banned_ip[] = '205.162.217.141'; 
    $banned_ip[] = '209.62.82.14'; 
    
    function is_banned($ip)
    {
    	$pattern = str_replace(array('*', '.'), array('\d+', '\.'), $ip);
    	return preg_match("~^{$pattern}$~", $_SERVER['REMOTE_ADDR']);
    }
    
    if (array_filter($banned_ip, 'is_banned'))
    {
    	echo 'Banned';
    }
    else
    {
    	echo 'Not banned';
    }
    
    
    PHP:
    Use an asterisk wherever you want to allow the range.
     
    nico_swd, Feb 24, 2008 IP
Thread Status:
Not open for further replies.