+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: [PHP] Ban and IP from you're webpage.

  1. #1
    Jonthefisherman is offline x10Hosting Member Jonthefisherman is an unknown quantity at this point
    Join Date
    Feb 2009
    Posts
    64

    [PHP] Ban an IP from you're webpage.

    Create a file called banned.php containing this:

    <?php
    $ip = getenv('REMOTE_ADDR');
    $email = "email@server.com"

    $ban1 = "000.000.000"
    $ban2 = "000.000.000"
    $ban3 = "000.000.000"
    $ban4 = "000.000.000"
    $ban5 = "000.000.000"
    $ban6 = "000.000.000"

    if($ip == $ban1 || $ip == $ban2 || $ip == $ban3 || $ip == $ban4 || $ip == $ban5 || $ip == $ban6)
    {
    echo "You have been banned from this website. To appeal this ban, contact the webmaster at $email."
    exit;
    }
    ?>
    Replace the 0's with the IP address you want to ban.

    Add the this code to all the pages on you're site you want this to apply to.
    ( Make sure the file extension of the page is .PHP | Example = index.php )


    <?PHP
    include("banned.php");
    ?>
    The code should now work, now to explain the code:


    $ip = getenv('REMOTE_ADDR'); - I am assigning the person's IP to the variable $ip. $ip is used when checking the IP against the banned list.

    $email = "email@server.com" - Change email@server.com to your e mail so people can contact you regarding the ban.

    $ban1 = "000.000.000" - Change the 0's to the person's IP you wamt to ban. That assigns the banned IP to the variable. It is the same with ban1 to 6

    if($ip == $ban1 || $ip == $ban2 || $ip == $ban3 || $ip == $ban4 || $ip == $ban5 || $ip == $ban6) - Checks the $ip variable against a list of banned ip's. If it matches, it echos and the user cannot view the site. However, if it does not match any IP on the list of banned IP addresses the user can view the webpage normally.
    If you want to ban anymore IP's, add another line to the banned list. (Remember to change the number)
    Then, edit the if statement.
    Last edited by Jonthefisherman; 02-22-2009 at 10:09 PM.

  2. #2
    zen-r's Avatar
    zen-r is offline Lord Of The Keys zen-r is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Location,Location. Nothing else matters ....apparently.
    Posts
    1,937

    Re: [PHP] Ban and IP from you're webpage.

    Pretty good, thanks.

    But I just hope that the spelling for the coding is better than the spelling you used for this thread title ;)

    "[PHP] Ban and IP from you're webpage."


    Please click my Reputation button (at the corner of this post) & make me -it costs you nothing!

    If I've traded services/credits with you, please remember to leave iTrader Feedback. Thanks.
    For great installation & servicing of Audio Visual systems & equipment
    inc. LCD & Plasma Screens, Loudspeakers, Projectors, Aerials & Satellite Dishes, Lighting effects & controllers, Hifi, Amplifiers, Surround Sound, Home Cinema & Video etc
    -: based around Plymouth, Devon, or anywhere in the southwest of the UK, visit :-

  3. #3
    Parsa44 is offline x10 Sophmore Parsa44 is an unknown quantity at this point
    Join Date
    Aug 2008
    Posts
    232

    Re: [PHP] Ban and IP from you're webpage.

    Thanks mate it will surley come in usefull, i have lots of IP's who i would love to ban =)

  4. #4
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: [PHP] Ban and IP from you're webpage.

    Dun wanna seem like I'm hijacking the topic, but if you aren't big on PHP and you're running a mostly HTML site, there's also this little addon for .HTAccess files:

    Code:
    order allow,deny
    deny from xxx.xx.x.x
    deny from xxx.xx.x.x
    deny from xxx.xx.x.x
    allow from all
    Replace xxx.xx.x.x with the ip you're blocking; can have as many or as few in there as you want.



    Only problem is it just returns 403 forbidden; I'm not sure if you can get it to return something like a nice "You've been banned" page

    Just a tidbit for Hardcore HTML sites out there


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  5. #5
    remie is offline x10Hosting Member remie is an unknown quantity at this point
    Join Date
    Sep 2006
    Posts
    14

    Re: [PHP] Ban and IP from you're webpage.

    Dont wanna be annoying but your php code is a bit inefficient and you cant ban more the 6 ips easily.
    My idea:
    Code:
    <?php
    $ips = array('0.0.0.0',
                        'add ips here');
    if(in_array($_SERVER['REMOTE_ADDR'],$ips))
    {
    echo "You have been banned from this website. To appeal this ban, contact the webmaster at $email."
    exit;
    }
    ?>

  6. #6
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Re: [PHP] Ban and IP from you're webpage.

    just an observation of the effectiveness of banning ip's in general...

    given that ip addresses are usually assigned dynamically by the ISP using some kind of DHCP, going to any effort to ban IP addresses seems like a waste of time to me, and may even be counterproductive in that you will probably end up locking out the wrong person, cause soon someone else will get the ip address?

    just a thought.

  7. #7
    lair360 is offline x10 Sophmore lair360 is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    200

    Re: [PHP] Ban and IP from you're webpage.

    Ahem....you guys do need to simplify the script and make it clean!
    Seriously...here is a better script...That I Made!!

    Code:
    <?php $banned = array (
    //Add your own IP address under this line.
    '122.2.13.1',
    '144.2.76.6',
    '166.2.76.6',
    // IP Address Ends Here.
    );
    $ip = GetHostByName($REMOTE_ADDR);
    if (in_array($ip,$banned)
    {
    echo 'You are banned from this site!';
    exit();
    } ?>
    Copyrighted By Lair360
    Last edited by lair360; 05-31-2009 at 06:22 PM.

  8. #8
    zen-r's Avatar
    zen-r is offline Lord Of The Keys zen-r is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Location,Location. Nothing else matters ....apparently.
    Posts
    1,937

    Re: [PHP] Ban and IP from you're webpage.

    Quote Originally Posted by fguy64 View Post
    just an observation of the effectiveness of banning ip's in general...

    given that ip addresses are usually assigned dynamically by the ISP using some kind of DHCP, going to any effort to ban IP addresses seems like a waste of time to me, and may even be counterproductive in that you will probably end up locking out the wrong person, cause soon someone else will get the ip address?

    just a thought.
    You make a good point, though it is still a reasonably effective, industry-wide method used to attempt to eliminate troublesome visitors to websites. There is little else most sites can do.

    I say reasonably effective, because many people have static or semi-permanent IP addresses. For example, as a cable customer my address hardly ever changes. Your point does highlight at least 2 things though;

    1) the banned IP addresses should be purged from the list after a reasonable period of time (1 month?)

    2) there should be a contact point or facility provided by the website to allow people to request that their IP be un-blocked if they can justify it.

    There is of course another good reason to question the effectiveness of banning IPs : more & more people now understand how to use a proxy, & thus bypass the ban by appearing to come from a different IP address!


    Please click my Reputation button (at the corner of this post) & make me -it costs you nothing!

    If I've traded services/credits with you, please remember to leave iTrader Feedback. Thanks.
    For great installation & servicing of Audio Visual systems & equipment
    inc. LCD & Plasma Screens, Loudspeakers, Projectors, Aerials & Satellite Dishes, Lighting effects & controllers, Hifi, Amplifiers, Surround Sound, Home Cinema & Video etc
    -: based around Plymouth, Devon, or anywhere in the southwest of the UK, visit :-

  9. #9
    damsch12 is offline x10Hosting Member damsch12 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    search in your mind...
    Posts
    81

    Re: [PHP] Ban and IP from you're webpage.

    i also think its not effective to ban an ip address, in my country our ip changes automatically every 12 hours, and you can also close the connection and open it again with a new ip.
    Last edited by damsch12; 05-31-2009 at 07:18 PM.

    Want to get listed in a web directory for free??, go here

  10. #10
    Linkz0rs's Avatar
    Linkz0rs is offline x10 Sophmore Linkz0rs is an unknown quantity at this point
    Join Date
    Feb 2009
    Location
    Virginia, USA
    Posts
    225

    Re: [PHP] Ban and IP from you're webpage.

    Quote Originally Posted by zen-r View Post
    There is of course another good reason to question the effectiveness of banning IPs : more & more people now understand how to use a proxy, & thus bypass the ban by appearing to come from a different IP address!
    There are some proxy blocking scripts out there.. Such as TOR Blacklist from proxy.org
    Although, not all of them are as effective as you'd hope them to be... But they do block SOME proxies.
    I see it a waste of time blocking every IP they switch to... Lol.
    If I really didnt want someone to see the site, I'd make it members only or something... And have Admin Approved signup... Of course, this could take some extensive coding, unless your using something that lets you do that.

+ Reply to Thread
Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers