Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 05-14-2003, 08:29 PM   #1 (permalink)
Boks
Registered User
 
Join Date: May 2003
Posts: 2
Boks is on a distinguished road
PHP ban IP range script

I know how to ban a single IP from visiting my site, but I need to ban a range.

I've got:

Code:
<?php
if (eregi ("123\.123\.123\.[1-100]", $HTTP_SERVER_VARS["REMOTE_ADDR"]))
{
print "Banned";
       exit();
}?>
but that seems to ban 123.123.123.1-255.

Any ideas how to do it or what I'm doing wrong? I've searched google (that's where I found the above code) and some script sites, but didn't have much luck finding what I need.

Thanks for any help
Boks is offline   Reply With Quote
Old 05-14-2003, 09:33 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
this should work =)
PHP Code:
<?php
$ip
="123.123.123.101";

if (
eregi("123\.123\.123\.([0-9]$|[0-9]{2}$|100$)",$ip))
{
  print 
"Banned";
  exit();
}
else
{
  print 
"passed";
}
?>
__________________
Mike
sde is offline   Reply With Quote
Old 05-17-2003, 07:24 AM   #3 (permalink)
Boks
Registered User
 
Join Date: May 2003
Posts: 2
Boks is on a distinguished road
I gave it a try, but didn't completly understand the code, however simple it may be . I don't know too much about php yet .

It probably doesn't make too much differene, but I I guess should have said that I wanted to ban the last 2 ranges 123.123.*.* . But when I modified the code, I couldn't get it to work properly. I'm pretty sure it was something I was doing wrong and not the actual code.

I ended up doing it like this:

PHP Code:
<?php
$ban_range_low
=ip2long("217.234.1.1");
$ban_range_up=ip2long("217.234.255.255");
$ip=ip2long($HTTP_SERVER_VARS["REMOTE_ADDR"]);
if (
$ip>$ban_range_low && $ip<$ban_range_up)
{
    print 
"Banned";
    exit(); 
}
?>
Thanks for your help though sde, much appreciated
Boks is offline   Reply With Quote
Old 05-17-2003, 07:39 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Or perhaps like this:
PHP Code:
<?php

if (eregi("123.123.([0-9]$|[0-9]{2}$|255$).([0-9]$|[0-9]{2}$|255$)",
            
ip2long($HTTP_SERVER_VARS["REMOTE_ADDR"])))
{
  print 
"Banned";
  exit();
}
else
{
  print 
"passed";
}
?>
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-18-2007, 01:04 AM   #5 (permalink)
yak
Recruit
 
Join Date: Jul 2007
Posts: 1
yak is on a distinguished road
ip range ban

Single ip ban or 3 different ranges of ip










<?php
// Addresses to block.
$range = array('0'=>'216.47.55.141',
'1'=>'72.47.147',
'2'=>'74.171',
'3'=>'58'
);

// Check current IP.
foreach($range as $list){
if(strstr($_SERVER['REMOTE_ADDR'], $list)){
echo 'Sorry page is down check back later';
exit();
}
}
?>
yak is offline   Reply With Quote
Old 07-18-2007, 10:21 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
wow, talk about digging up a 4 year old post!

anyhow, your example is wrong. your "if" statement would pass regardless of where the location of the partial IP is. strstr just returns the string up until the position of the match.

example: 1.2.3.58, 1.2.74.171

both of those IPs would be denied access. you need to use regular expressions to indicate that '58' is the beginning of what you are trying to match.
__________________
Mike
sde is offline   Reply With Quote
Old 07-18-2007, 10:54 AM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
if you didn't want to use regular expressions, and the IPs in your IP array always started at the beginning of the IP, then you could do something like this:
PHP Code:
<?php
function checkIP($ip_to_match$ip_array) {

  
// make sure this is an array before we use foreach
  
if (is_array($ip_array)) {

    
// loop through ip array
    
foreach ($ip_array as $ip) {

      
// first test if there is a match, then test if the match starts at the beginning
      
if (strpos($ip_to_match$ip)===0) {
        return 
true;
      }
    }
  }

  return 
false;
}

$ban_array = array(
  
'216.47.55.141',
  
'72.47.147',
  
'74.171',
  
'58'
);

if (
checkIP($_SERVER['REMOTE_ADDR'], $ban_array)) {
  echo 
"This page is not accessible.";
  exit;
}
?>
:: modified from kelvin's suggestion below ::
__________________
Mike
sde is offline   Reply With Quote
Old 08-03-2007, 05:26 AM   #8 (permalink)
Kelvin
Recruit
 
Join Date: Aug 2007
Posts: 1
Kelvin is on a distinguished road
Just the function I needed, cheers sde.

1 slight possible improvement is removing the strstr, and instead checking explicitly (===) for 0 on strpos.

PHP Code:
function checkIP($ip_to_match$ip_array) {

  
// make sure this is an array before we use foreach
  
if (is_array($ip_array)) {

    
// loop through ip array
    
foreach ($ip_array as $ip) {

      
// first test if there is a match, then test if the match starts at the beginning
      
if (strpos($ip_to_match$ip)===0) {
        return 
true;
      }
    }
  }

  return 
false;

Kelvin is offline   Reply With Quote
Old 08-03-2007, 06:14 PM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
nice catch!
__________________
Mike
sde is offline   Reply With Quote
Old 08-03-2007, 08:45 PM   #10 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Yeah, that's what's always bugged me about the weak-typing nature of PHP - I'm never quite sure if what I'm seeing is a 0 or FALSE, 1 or TRUE.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 08-04-2007, 12:37 AM   #11 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
Why not put a CIDR block in .htaccess?
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 08-04-2007, 08:05 AM   #12 (permalink)
unknownsheep
Recruit
 
Join Date: Aug 2007
Posts: 3
unknownsheep is on a distinguished road
PHP Code:
$ip $_SERVER['REMOTE_ADDR'];
$ip str_replace(".","",$ip);

$ip_from "192.168.0.0";
$ip_to "192.168.255.255";

$ip_from str_replace(".","",$ip_from);
$ip_to str_replace(".","",$ip_to);

if (
$ip $ip_from || $ip $ip_to) { die("Denied"); } 
Shouldn't that work?
unknownsheep is offline   Reply With Quote
Old 08-04-2007, 09:01 AM   #13 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 745
DJMaze is on a distinguished road
It costs yet another member 10 lines of code for IP banning.
And you all forgot to send the 403 HTTP Status code header, so again.....

Why not just put a CIDR block in .htaccess?

It's as easy as writing 1 line of code.
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 08-04-2007, 02:12 PM   #14 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Hmm... considder your code once more, then you'll soon realise a flaw in it.
Else try to match the ip: 213.1.34.120 in your code.

The easy way would be to use CIDR blocking.
Code:
Deny from 192.168.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 08-04-2007 at 02:23 PM.
redhead is offline   Reply With Quote
Old 08-04-2007, 03:30 PM   #15 (permalink)
unknownsheep
Recruit
 
Join Date: Aug 2007
Posts: 3
unknownsheep is on a distinguished road
Quote:
Originally Posted by redhead View Post
Hmm... considder your code once more, then you'll soon realise a flaw in it.
Else try to match the ip: 213.1.34.120 in your code.

The easy way would be to use CIDR blocking.
Code:
Deny from 192.168.
A flaw in what?
unknownsheep is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
php script that can upload binary data infinite_root PHP 9 08-27-2004 07:03 PM
PHP Comes of Age sde Code Newbie News 0 04-14-2004 12:41 PM
sending POST multiple post requests in one php script. sde PHP 2 08-09-2003 06:10 PM
I need to learn PHP Nitro PHP 9 06-28-2003 12:24 PM
Question about a php prediction script(Great Site!) Geetazz PHP 15 06-12-2002 06:37 PM


All times are GMT -8. The time now is 07:54 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting