1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

block ip address using php code

Discussion in 'PHP' started by plusd, Mar 20, 2006.

  1. bhagwat_banna

    bhagwat_banna Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #21
    can u please telle how can we do this via .htaccess files.
    SEMrush
     
    bhagwat_banna, Jan 1, 2009 IP
    SEMrush
  2. onehundredandtwo

    onehundredandtwo Guest

    Messages:
    56
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #22
    http://www.javascriptkit.com/howto/htaccess5.shtml

    But what if you can't use .htaccess?
     
    onehundredandtwo, Jan 2, 2009 IP
  3. zaphodb777

    zaphodb777 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #23
    I've got a somewhat more complex GPL PHP script you may like to try. It's a anti-spam, anti-hack, and definitely a domain/IP range filtering script for all php websites.

    It's called ZB Block. It does not require any Apache security modules, MySQL or even the rewrite module for .htaccess. However, it will give you all the functionality you are looking for, and much more. The only thing it does need, outside of pure PHP 4 or 5, is .htaccess to be able to deny a directory to all, and ftp (or like) access to get around the blockage to edit files.

    You can read more about it at http://www.spambotsecurity.com/zbblock.php . You can download it from http://www.spambotsecurity.com/zbblock_download.php .

    Zap :)

    P.S. Support forum on site. Hope to hear from you soon!
     
    zaphodb777, Aug 27, 2009 IP
  4. cham

    cham Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #24
    The above are really helpful - how would I embed the php code in html?
     
    cham, Apr 5, 2010 IP
  5. superscript

    superscript Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #25
    I got the same problem, I run a local community announcement site, how can I ban every other country's ip range?
    I'm getting heavily hammered with spam.

    I started banning individual ips, but then it got ridiculous, the spammers just get a new ip, but i noticed they all start with the same two numbers.
    That's when I thought about banning the ip range.
     
    superscript, Dec 4, 2010 IP
  6. spamtrawler

    spamtrawler Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #26
    Hi superscript,

    This is a post I made in a different forum yesterday:

    "You can actually do quite a few things to slow them down and eventually stop a lot of spam coming through webforms.

    Image captchas showing text are unfortunately not unbreakable anymore with some bots using:
    - OCR
    - Human Captcha Solving services

    Re-Captcha can be broken with a success rate of 23% (as of researchers)

    Captcha implementations like:
    - Microsoft Assira
    still keep a lot of automated spam at bay

    What else can be done is:
    - IP Filtering (DNSBLs, ProjectHoneypot, Local IP Blacklists and so on)
    - IP2Country detection (Only allow visitors from countries you are expecting legit business from)
    - UserAgent tracking and blocking (block certain UserAgents which keeps Spammers out even if they come back with a different IP)
    - Referrer tracking and blocking (block certain Referrer which keeps Spammers out even if they come back with a different IP)
    - Charset detection (Do not allow posts if parameters contain a certain character set)
    - HTTP Header checks
    - Block URL Attacks that show a certain pattern
    - Block Ranges of known "Bad IPs"

    These steps combined with a captcha implementation like Assira to let visitors who are human and have been detected, unblock themselves, actually helps a lot.

    Hope this post was usefull :)"

    If you need any help, please feel free to contact me.
     
    spamtrawler, Dec 4, 2010 IP
  7. CPAPubMichael

    CPAPubMichael Peon

    Messages:
    231
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #27
    Here is an really simple script.

    
    
     <?php
    
       $ip = $_SERVER['REMOTE_ADDR'];
    
       if($ip == "00.00.00.00"){
          echo "<font color='red'>Ooops, your ip address has been banned from this website.</font>";
          exit();
       }
    
     ?>
    
    
    PHP:
     
    CPAPubMichael, Dec 4, 2010 IP
  8. superscript

    superscript Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #28
    THANKS!! CPAPubMichael. I'm gona try this :) can this be used for ip range?
     
    superscript, Dec 5, 2010 IP
  9. CPAPubMichael

    CPAPubMichael Peon

    Messages:
    231
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #29
    I believe you can do:

    
    
    <?php
    
       $ip = $_SERVER['REMOTE_ADDR'];
    
       if($ip == "00.00.00.00" - "11.11.11.11"){
          echo "<font color='red'>Ooops, your ip address has been banned from this website.</font>";
          exit();
       }
    
     ?>
    
    
    PHP:
    This will then ban all IP addresses what are between 00.00.00.00 and 11.11.11.11

    I hope i helped.

    I have not tried this but i believe it would work.

    Thanks.
     
    CPAPubMichael, Dec 5, 2010 IP
  10. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #30
    This will not work.

    If you want to block an IP by its range, you need to use binary shifts and/or ip2long to convert the octets.
     
    lukeg32, Dec 6, 2010 IP
  11. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #31
    use this code

    I hope this one helps
     
    Last edited: Dec 6, 2010
    drctaccess, Dec 6, 2010 IP
  12. CPAPubMichael

    CPAPubMichael Peon

    Messages:
    231
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #32
    Like i said, i had not tried this. I have never banned by range.
     
    CPAPubMichael, Dec 7, 2010 IP
  13. SIGNAGI

    SIGNAGI Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #33
    how I can black OPERA MINI?
     
    SIGNAGI, Feb 20, 2011 IP
  14. SIGNAGI

    SIGNAGI Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #34
    can you halp me? please :(
     
    SIGNAGI, Feb 20, 2011 IP
  15. IProx

    IProx Well-Known Member

    Messages:
    1,749
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    130
    #35
    BlockScript allows you to block Opera Mini proxies as well as ranges of IP addresses.
     
    IProx, Aug 13, 2011 IP
  16. LarcenIII

    LarcenIII Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #36
    I highly recommend blockscript.org

    They offer finite control, that is, if a paid option is suitable for you.

    But it's quality is way beyond anything you can do alone.
     
    LarcenIII, Aug 13, 2011 IP
  17. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #37
    have you tried to ask what might be the issue in your htaccess file? htaccess is much better in blocking ips..
     
    JohnnySchultz, Aug 14, 2011 IP
  18. Technoslab

    Technoslab Peon

    Messages:
    46
    Likes Received:
    3
    Best Answers:
    3
    Trophy Points:
    0
    #38
    Many devices and users could be using a single IP to connect to your website. Banning IP(s) is not a good option at all times.
     
    Technoslab, Aug 16, 2011 IP
  19. paluster

    paluster Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #39
    hi all,
    i need the script in this topic.

    
    $sql = mysql_query("SELECT ip FROM ip_blocked");
    while ($row = mysql_fetch_array($sql)) {	
      $targetAddr = array ($row['ip']);
    }
    
    
    foreach($targetAddr as $var) {
      if (preg_match('/^' . $var . '$/', $_SERVER['REMOTE_ADDR'])) { 
      echo "ip blocked ";
     } else {
      echo "ok";
     }
    }
    
    Code (markup):
    If the IP in database are all different from the visitor's IP I obtain the "ip blocked" message
    If there aren't IP in database I obtain "Notice: Undefined variable: targetAddr + Warning: Invalid argument supplied for foreach()"
    If the IP in database is the same of visitor's IP OK (i try in localhost)
    If the IP in database is 127.0.0.* OK (i try in localhost)

    How can I fix the first and second issue?

    Thanks! regards!!
     
    paluster, Dec 9, 2011 IP
  20. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #40
    All it takes is moving a single close brace from after
    $targetAddr = array ($row['ip']);
    to the end of the code you show. If there's no row, $targetAddr never gets set (so it doesn't exist) so the code using it never gets executed. You have the foreach using a variable that was never set if there are no rows.
     
    Rukbat, Dec 10, 2011 IP