Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-18-2011, 10:51 PM   PM User | #1
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Block IP range

I need a method of blocking and IP range, E.G. 115.125.12.*

So anything starting with 115.125.12 would be blocked. What would the easiest way of doing this be?
tomharto is offline   Reply With Quote
Old 10-18-2011, 11:59 PM   PM User | #2
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
On just one page or what?

PHP Code:
<?php

$range 
= array();

$range "99.99.99";

$user $_SERVER['REMOTE_ADDR'];
$users explode("."$user);

$users_a $users[0] . "." $users[1] . "." $users[2];

if ( 
in_array $users_a$range ) )
{
// do something..


}
?>
Adee is offline   Reply With Quote
Old 10-19-2011, 12:25 AM   PM User | #3
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
I would use htaccess unless you want to present something more than a HTTP 403 error.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 10-19-2011, 12:30 AM   PM User | #4
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
well what i do is if a users IP is banned i redirect them to a page saying "your banned" and the details about it.

Regarding the .htaccess the IPs are stored in a database, is it possible to fill the htaccess file from a database or isnt that doable?
tomharto is offline   Reply With Quote
Old 10-19-2011, 12:56 AM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I like to over-complicate things myself. I'd write a cidr class that lets me compare a provided address to a subnet range.
Hmm, offhand I'm not sure if you can link the ip lookup into .htaccess. I know you can do the authentication for basic and whatnots (but you can do that from PHP as well), but for actual IP addresses, I'm not too sure.
Fou-Lu is offline   Reply With Quote
Old 10-19-2011, 03:03 AM   PM User | #6
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
Quote:
Originally Posted by tomharto View Post
well what i do is if a users IP is banned i redirect them to a page saying "your banned" and the details about it.

Regarding the .htaccess the IPs are stored in a database, is it possible to fill the htaccess file from a database or isnt that doable?
What I gave you is viable. You could redirect them with it. You could probably make a script to write an .htaccess file for you with the IPs + necessary tags and have a cronjob or something to update it to include new IPs you add
Adee is offline   Reply With Quote
Old 10-19-2011, 08:30 AM   PM User | #7
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Im gonna look at putting your code into what i already have, ill let you know what happens when i do it later on today
tomharto is offline   Reply With Quote
Old 10-19-2011, 03:26 PM   PM User | #8
Cjwinnit
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 0
Thanked 1 Time in 1 Post
Cjwinnit is an unknown quantity at this point
I wrote a script for this exact purpose a week or two ago

http://www.codingforums.com/showthread.php?t=239805
Cjwinnit is offline   Reply With Quote
Users who have thanked Cjwinnit for this post:
tomharto (10-19-2011)
Old 10-19-2011, 05:03 PM   PM User | #9
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
That looks like it would do the job , im just thinking now, how would i put it into my current script?
PHP Code:
$IP $_SERVER['REMOTE_ADDR'];
$curTime time(); //ban_start     ban_end
$banList "SELECT * FROM phpbb_banlist WHERE ban_start < $curTime AND ban_end > $curTime AND ban_ip = '$IP' OR ban_start < $curTime AND ban_end = 0 AND ban_ip = '$IP'";

$banRes  mysql_query($banList);
if ((
mysql_num_rows($banRes)) > 0)
{
    
$deny = array();
    while (
$row mysql_fetch_array($banRes))
    {
        
$deny[] = $row['ban_ip'];
        
$deny[$row['ban_ip']] = $row['ban_end'];
    }
    
    
    if (
in_array($_SERVER['REMOTE_ADDR'], $deny))
    {
        
$_SESSION['until'] = date("d-m-Y"$deny[$_SERVER['REMOTE_ADDR']]);
        
header ("Location: banned.php");
        die();
    }

The IP's it would return are in the format xxx.xxx.xxx.xxx or xxx.xxx.xxx.*
tomharto is offline   Reply With Quote
Old 10-19-2011, 05:16 PM   PM User | #10
Cjwinnit
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 0
Thanked 1 Time in 1 Post
Cjwinnit is an unknown quantity at this point
Hi. 2 questions:

1/ By "that", do you mean mine?

2/ You said in the form "xxx.xxx.xxx.xxx" and "xxx.xxx.xxx.*"? so you'd have a list of banned individual IP's and banned ranges together in the same banlist?
Cjwinnit is offline   Reply With Quote
Old 10-19-2011, 05:19 PM   PM User | #11
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Yeah i meant your . Yeah there all in one list, would that be a problem?
tomharto is offline   Reply With Quote
Old 10-19-2011, 05:25 PM   PM User | #12
Cjwinnit
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 0
Thanked 1 Time in 1 Post
Cjwinnit is an unknown quantity at this point
Not necessarily.

I would say that having it in "xxx.xxx.xxx.0/bla" form is a bit better as it's a standard way of storing it - I understand if it's a bit of work to change though.

As examples,
"192.168.0.*" is the same as "192.168.0.0/24".
"192.168.*.*" is the same as "192.168.0.0/16".
"192.*.*.*" is the same as "192.168.0.0/8".

If you wanted to ban only the IP "200.15.6.7" you would write "200.15.6.7/32"

I'll work on your script
---------------
is "$deny[]" in the loop the banlist in array form?

Last edited by Cjwinnit; 10-19-2011 at 05:29 PM..
Cjwinnit is offline   Reply With Quote
Old 10-19-2011, 05:32 PM   PM User | #13
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Okay, well that wouldnt be much to change yet at all so i can do it that way, how would i put your functions into my script cause mines just a simple in_array
tomharto is offline   Reply With Quote
Old 10-19-2011, 05:39 PM   PM User | #14
Cjwinnit
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 0
Thanked 1 Time in 1 Post
Cjwinnit is an unknown quantity at this point
1/ Put the two functions in your script.

2/ Have your script get the ban list out of the MySQL database and store it in an array (your script does this well as far as I can see ). For sake of argument, let's call the array "$banlist".

3/ Change:
PHP Code:
if (in_array($_SERVER['REMOTE_ADDR'], $banlist)) { **DO STUFF** } 
to:
PHP Code:
foreach($banlist as $val

if(
isipinsub ($_SERVER["REMOTE_ADDR"], $val)){ **DO STUFF** } 

----

In fact you could store individual IP's in your database even if you forgot to put "/32" at the end. It wouldn't take too long to write a quick script to stick "/32" on the end of an array entry if it didn't find a slash in it.

Last edited by Cjwinnit; 10-19-2011 at 06:06 PM..
Cjwinnit is offline   Reply With Quote
Old 10-19-2011, 05:40 PM   PM User | #15
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 794
Thanks: 91
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Thanks a lot , a lot easier than i was thinking it was gonna be :P
tomharto is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:44 PM.


Advertisement
Log in to turn off these ads.