Block IP address from PHP script

Discussion in 'PHP' started by champ_rock, Jun 6, 2007.

  1. #1
    hi

    i want to know what code should i use to block certain Ip addresses from accessing my site.?? i do not want to use .htaccess for it as it is currently disabled and i cannot use it

    therefore a php code doing that work would be fine??

    can somebody help me out?
     
    champ_rock, Jun 6, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?php
    $banned = array(
    	'127.0.0.1',
    	'192.168.0.1'
    );
    
    if( in_array( $_SERVER['REMOTE_ADDR'], $banned ) )
    {
    	die("Your banned, fuck off");
    }
    ?>
    
    PHP:
    NEVER ban 127.0.0.1
     
    krakjoe, Jun 6, 2007 IP
    champ_rock likes this.
  3. champ_rock

    champ_rock Peon

    Messages:
    3,349
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks.. will give it a try by taking the IP addresses from a text file. i will edit that code

    reps for u
     
    champ_rock, Jun 6, 2007 IP
  4. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #4
    1. Make a table in database with banned IPs.
    2. Insert a script @ beginning of web page which checks the ip is already listed as banned in the table and if exists show "you are banned"

    This is the traditional way.
     
    olddocks, Jun 7, 2007 IP
  5. Gursimran

    Gursimran Well-Known Member

    Messages:
    1,385
    Likes Received:
    53
    Best Answers:
    0
    Trophy Points:
    160
    #5
    Gursimran, Jun 7, 2007 IP
    champ_rock and ahref like this.
  6. champ_rock

    champ_rock Peon

    Messages:
    3,349
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks a lot but the thing is that i was not looking for a database script. i was looking for something which i can use without any hassles
     
    champ_rock, Jun 8, 2007 IP
  7. ahref

    ahref Well-Known Member

    Messages:
    1,123
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    170
    #7
    Nice script gurusimran rep added
     
    ahref, Jun 8, 2007 IP