Results 1 to 7 of 7
  1. #1

    Blocking IP Script [PHP]

    I was just wondering what is the best method of blocking more than one IP address using PHP?

  2. #2
    Join Date
    Feb 2005
    Maybe this works..

    $ip = $_SERVER['REMOTE_ADDR'];
    $to_ban = file ('ip.dat');
    while(list($key,$val) = each($to_ban)) {
    if($ip == trim($val)) {
    echo "You are banned!!";

    the ip.dat are supposed to contain the banned IP's.. one each line..

    or maybe this:

    $ip = $_SERVER['REMOTE_ADDR'];
    $to_ban = array("", "");
    if (in_array("$ip", $to_ban)) {
    die("You are banned!!");
    Last edited by UrlGuy; 06-25-2005 at 08:34 PM.

  3. #3
    as for me, better to use .htaccess, but everything depends on needed result
    HostNodeList Web Host Directory, DEV.INTOEX.COM - products for online business

    Experienced web-developer | PHP | Smarty | Zend | Databases | Graphic design - looking for long-time relationship

  4. #4
    Join Date
    Jul 2001
    Glasgow, Scotland
    Or even better, if you're running Unix/Linux;
    Use host.allow to block server wide access to IPs of your choice.
    The paranoid, you can even set up a firewall to block certain IPs.

    But if you really want PHP to do it, I support UrlGuy's example of using REMOTE_ADDR and array to store them.
    Also be sure to google for HTTP_X_FORWARDED_FOR, which is a variable that lists the real IP address of users from proxies.
    My development blog - (un)Interesting codesnippets and the occational code-related rant!

  5. #5
    Ok, thanks alot. It helped alot!

  6. #6
    I think it's better to ban the host cause lots of people dont have a static IP

  7. #7
    I'm not 100% on how the host system works, but from what i've seen, tons of people have the same host (with a different IP based prefix)... so banning a host could cause some undesierable effects. Also IP banning isn't exactly great because of the above stated reason.
    Mind the gap.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts