Need help with my PHP (Banned IP / Random Redirect) script

Discussion in 'PHP' started by X_X_ROB_X_X, Mar 20, 2008.

Thread Status:
Not open for further replies.
  1. #1
    I've been working on a PHP script that will first look to see if a users IP is banned. If banned it redirects them to a predesignated URL. If not it redirects them to a diferent URL. Up to here I have gotten to work fine.

    Want I want to do now is instead of sending the non banned users to just one URL, I am trying to have it send them to a randomly selected URL in an array. This is where I am having problems.

    Below is an example of my code so far. Anyone see what I am doing wrong or have any suggestions on a better way to do this without using SQL?


    
    <?php 
    $banned_ip = array(); 
    $banned_ip[] = '10.114.108.17'; 
    $banned_ip[] = '10.100.46.10'; 
    $banned_ip[] = '69.72.244.98'; 
    $banned_ip[] = '167.206.128.131'; 
    $banned_ip[] = '205.162.217.141'; 
    $banned_ip[] = '76.90.3.205'; 
    $banned_ip[] = '77.91.224.6'; 
    $banned_ip[] = '82.80.249.145'; 
    
    foreach($banned_ip as $banned) 
    {  
        $ip = $_SERVER['REMOTE_ADDR']; 
        if($ip == $banned)
        {  
            header("location: http://www.ALL-BANNED-IPS-ARE-SENT-TO-THIS-URL.com"); 
            die();//abort the script on redirect. just in case.
        }
    }  
    //if the IP is not banned then I want people to be randomly sent to one of the URLs listed below
    $urls = array("www.THIS-SITE.com", 
                  "www.OR-THIS-SITE-1.com", 
                  "www.OR-THIS-SITE-2.com", 
                  "www.OR-THIS-SITE-3.com", 
                  "www.OR-THIS-SITE-4.com", 
                  "www.OR-THIS-SITE-5.com"); 
    $url = $urls[array_rand($urls)]; 
    header("location: http://$url"); 
    ?> 
    
    Code (markup):
     
    X_X_ROB_X_X, Mar 20, 2008 IP
  2. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    Something like :

    $url[1] = 'http://www.THIS-SITE.com';
    $url[2] = 'http://www.That-SITE.com';
    $url[3] = 'http://www.SITE.com';
    $url[4] = 'http://www.blahblah.com';

    etc etc..
    listing all random sites with a number.
    Then run a random function to randomly grab one like this:

    $i=rand(1,50);
     
    ezprint2008, Mar 20, 2008 IP
Thread Status:
Not open for further replies.