How do you ban people based on IP addresses

Discussion in 'PHP' started by Imozeb, Jan 24, 2010.

  1. #1
    How do you redirect people based on IP addresses using PHP if they cannot enter a password and redirect people that are not banned straight to a home page?
     
    Imozeb, Jan 24, 2010 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Their IP address, if they are not using a proxy, is in $_SERVER['REMOTE_ADDR'].
     
    SmallPotatoes, Jan 24, 2010 IP
    Imozeb likes this.
  3. Bananasphere

    Bananasphere Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Something like this:

    
    $ip = $_SERVER['REMOTE_ADDR'];
    $query = mysql_query("SELECT * FROM banned_ips WHERE ip = '$ip'");
    
    if( mysql_num_rows($query) == 0 )
    {
        header("Location: http://example.com/index.php");
    }
    else
    {
        die("Sorry, you're banned!");
    }
    
    PHP:
    You would have to have a table with banned IP's in them, of course. Hope that helps.

    - Bananasphere :)
     
    Last edited: Jan 24, 2010
    Bananasphere, Jan 24, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    or..

    <?php
    //banned ips
    $bannedips = array("123.2334234.23", "34345.24354.34");
    
    if(in_array($_SERVER['REMOTE_ADDR'], $bannedips)){
    
    echo "Your banned";
    
    } else {
    
    header("Location: http://example.com/index.php");
    
    }
    ?>
    PHP:
     
    danx10, Jan 24, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ~Problem Resolved


    Thanks everyone. Your code worked danx10!

    Thanks again! :)


    ~Imozeb
     
    Imozeb, Jan 25, 2010 IP