Shoutbox

Blocking an IP - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Blocking an IP (/showthread.php?tid=23497)

Blocking an IP by DJeX on 04-09-2004 at 09:45 PM

Does anyone know of a good IP blocker in php that will block IP's from your site?


RE: Blocking an IP by Digirat on 04-09-2004 at 09:58 PM

why don't you use htaccess? i'm assuming you're already running apache due to the php you're asking for


RE: Blocking an IP by sock on 04-10-2004 at 02:27 PM

IIS has (crappy) PHP support too... :p


Anyway, this code should work (although .htaccess is a good alternative):


<?php
  $ip = getenv("REMOTE_ADDR");
  $proxy = getenv("HTTP_X_FORWARDED_FOR");
  if ($proxy) {  // HTTP proxy used
    $pos = 0;
    while (($pos < strlen($proxy)) && ($proxy{$pos} != ','))
      $pos++;
    $ip = substr($proxy,0,$pos);
  }
  if ($ip == "1.2.3.4")
    die("Sorry, you are not cool enough to view this page.");
?>



Just change the IP address in red to the one you wish to block. :p If you want to block more than one IP address, you can use || (or) in the IF condition. If you want to block lots of them, you should probably use a better method of matching... Like using an array or something. :-/ Or just use .htaccess. :refuck:

Anyway, you can place the code at the top of the page, or perhaps under the <BODY> tag.


RE: Blocking an IP by Wabz on 04-10-2004 at 04:18 PM

This might help


RE: Blocking an IP by DJeX on 04-10-2004 at 04:34 PM

Where can I edit the htaccess? Or do I have to make one?


RE: Blocking an IP by Anubis on 04-10-2004 at 04:37 PM

Couldn't a JavaScript code do the same if PHP is unsupported?


RE: Blocking an IP by reisyboy on 04-10-2004 at 04:40 PM

How can you block IP on ur PC? Is it in the HOSTS?


RE: Blocking an IP by sock on 04-10-2004 at 05:28 PM

quote:
Originally posted by DJeX
Where can I edit the htaccess? Or do I have to make one?
You need to make one... You can make several ones and put them in different directories. But I don't know much about .htaccess so I can't help you with that really. :p


quote:
Originally posted by anubis_kree_
Couldn't a JavaScript code do the same if PHP is unsupported?
Theoretically, it could do something of the sort, but it would be quite stupid to do it that way. :p You'd have to print the IP address in the code somewhere, which again requires PHP (or other server-side script)... And the banned user could still read the source or simply disable JavaScript. It's stupid. :p


quote:
Originally posted by reisyboy
How can you block IP on ur PC? Is it in the HOSTS?
No, that simply acts as DNS cache which overrides DNS lookups on your computer. It can be abused to block access to certain domain names from your PC (by pointing them to localhost or something), but not actual IP addresses. You'd have to use a firewall to do that. :-/
RE: Blocking an IP by Anubis on 04-10-2004 at 05:36 PM

quote:
Originally posted by sock
Theoretically, it could do something of the sort, but it would be quite stupid to do it that way. You'd have to print the IP address in the code somewhere, which again requires PHP (or other server-side script)... And the banned user could still read the source or simply disable JavaScript. It's stupid.
Not if they guy's a n00b, but I see your point...

RE: Blocking an IP by stonesour on 04-10-2004 at 06:43 PM

I use this

code:
<?php


function ip_check()
{
    // Addresses to block.
    $block_ip[] = '203.217.64.174';
    // Check current IP.
    if (in_array($_SERVER['REMOTE_ADDR'], $block_ip))
    {
        return false;
    }
    else
    {
        return true;
    }
}
?>


Then in your index file or whatever put this include
code:
<?php    include 'bannedip.php';    if (!ip_check())   
{     
header( "Location: http://ng.audiodragon.net/bannage.php");
}
?>


Change the header location bit to the redirect site, bannedip.php to the name of your script.

You can also just display text using the echo command..
RE: Blocking an IP by sock on 04-10-2004 at 08:04 PM

Here's an adaptation of stonesour's ip_check(), with strict proxy support:


<?php
function ip_check()
{
  // Addresses to block
  $block_ip[0] = "64.246.30.37";
  $block_ip[1] = "207.46.249.252";
  $block_ip[2] = "212.143.162.198";

  if (in_array($_SERVER["REMOTE_ADDR"], $block_ip))
    return false;
  if (in_array($_SERVER["HTTP_CLIENT_IP"], $block_ip))
    return false;
  if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
    $proxy = explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]);
    $proxy = str_replace(" ","",$proxy); $i = 0;
    while ($proxy[$i]) {
      if (in_array($proxy[$i], $block_ip))
        return false;
      $i++;
    }
  }
  return true;
}
?>



This one should also ban people who use standard HTTP proxies... Note that the function returns TRUE if the user is allowed entry, and FALSE if s/he is banned.

Does anyone know how to fill the $block_ip array more efficiently? :p


RE: Blocking an IP by reisyboy on 04-10-2004 at 08:34 PM

quote:
Originally posted by sock
quote:

Originally posted by reisyboy
How can you block IP on ur PC? Is it in the HOSTS?


No, that simply acts as DNS cache which overrides DNS lookups on your computer. It can be abused to block access to certain domain names from your PC (by pointing them to localhost or something), but not actual IP addresses. You'd have to use a firewall to do that. 

Ahh right right right, ok well thats good enough for me, i want to restrict certian websites on certian computers (Sisters been asked to by dad) So ill just put the domians i want blocked in her pc's HOSTS

RE: Blocking an IP by CookieRevised on 04-11-2004 at 02:49 AM

quote:
Originally posted by anubis_kree_
Couldn't a JavaScript code do the same if PHP is unsupported?
No, you can't in JavaScript. It's useless... Like sock said: the user could disable the javascript and the block is lifted...
Furthermore, JavaScript is client-side... PHP is server-side... You can only block someone/IP's with a server-side language...
RE: Blocking an IP by DJeX on 04-11-2004 at 03:35 AM

I decided to use .htaccess to block people :mipdodgy:

.htaccess is great!