Jump to content

ip php blocking on single page


freelance84

Recommended Posts

Could somebody please point me in the right direction....

 

I want to block the US, AUS, NZ, CAN, IRE from the index.php of my site. But not the rest of the site.

 

The following site (like many others) provides a pretty neat list for the htaccess http://www.ipinfodb.com/ip_country_block.php

 

I did a test. I got the list for blocking the UK. After copying the UK list to my htaccess i couldn't view my site.

 

The thing i don't get is... my ip address was not in the list but i was still blocked,

 

The 1st 2 sets from my ip are '2.100'

 

The only ip's in the list starting with 2 are:

deny from 2.24.0.0/13

deny from 2.96.0.0/13

deny from 2.120.0.0/12

deny from 2.136.0.0/13

 

How exactly is this all working? And what's the best way of blocking the above countries from just my index.php?

 

Any links to manuals or anything here would be great...

 

 

Thank You.

 

John

Link to comment
Share on other sites

It comes from my Cisco Systems studies. The /13 is a subnet mask, represented in bits, so the mask would be 255.248.0.0. I convert the mask and the address to binary, and apply the mask to the address. It's difficult to explain the calculation without being able to demonstrate it, but the mask defines the network and host ranges of the address, as well as determining how many subnets are available with that particular mask.

 

At any rate, the IP address range for that subnet is 2.96.0.0 - 2.103.255.255, with the lowest address being the subnet's network address and the highest being the subnet's broadcast address.

Link to comment
Share on other sites

Ah ok... that sounds reasonably complex.

 

What would you suggest then:

When I use .htaccess to block the USA and a few other countries it slows the site down quite noticeably. As the site requires a username and password, the use of .htaccess to block certain countries is no good: Once the member is logged in there is no point checking what country they are from.

 

 

I thought the best way would be to use a database:

Upon load of the index.php, the 1st thing it is instructed to do is use an in_array against all the returned ip addresses in the db table. Is this going to be possible if I have to run a complex calculation of each ip address to discover the ip range?

Link to comment
Share on other sites

I've never been a big fan of using .htaccess rules or anything like that for IP based decisions; that job should be the responsibility of a router/firewall. If it's the only option you have however, I guess you either have to do that, or use maybe use a "pick your country" page and store a cookie. It depends on whether you want the block to be an actual block, or just a redirect.

Link to comment
Share on other sites

...It depends on whether you want the block to be an actual block, or just a redirect.

 

I don't want certain countries to be able to login until I'm ready for them: So I would ideally like people from those said countries to not see the login form. As an additional check I would also run the same check on the authenticate script.

 

Here's a mysql query statement that would calculate that -

select inet_ntoa(inet_aton('2.96.0.0') + (pow(2, (32-13))-1));

 

For this query, the 2.96.0.0 is the users ip address, but should any of pow(2, (32-13))-1) change per query? Or are these numbers always fixed?

Link to comment
Share on other sites

Hmm.. well as i do not know the range to start with...

 

Using the INET_ATON() function i can turn the IP into a numeric value.

 

If the following numeric ip range exists in a table:

 

"47710208","48234495","UK","UNITED KINGDOM"

 

And the numeric value of an ip to test was "47722222".

 

How would I check to see if the ip to test was in this range.... what would the mysql query look like?

(i appreciate this thread could now be seen as being in the wrong section)

Link to comment
Share on other sites

Ah... hang on. Still confused here...

 

"47710208","48234495","UK","UNITED KINGDOM"

And the numeric value of an ip to test was "47722222".

 

The only input i shall have at the start is the users ip address, eg: 47722222.

 

To use the 'BETWEEN min AND max' function i will need both the min and max, this would mean I would need to return all the ip's in one search, then with each returned result make a new query.

 

Is there a better way of doing this? Might i as well be placing all the numeric values from the table into one giant array in an included .php file and using in_array instead? Especially as the above way needs to return all the results into a mysql array anyway (thus taking up RAM on the server).

Link to comment
Share on other sites

<?php
$the_ip = $_SERVER['REMOTE_ADDR'];
$query = "SELECT inet_ntoa(int_ip_low) as ip_low, inet_ntoa(int_ip_high) as ip_high FROM ip WHERE inet_aton('$the_ip') BETWEEN int_ip_low AND int_ip_high";
$result = $mysqli->query($query);
if($result->num_rows){
$row = $result->fetch_assoc();
echo "Your IP: $the_ip is between {$row['ip_low']} and {$row['ip_high']}";
} else {
echo "Your IP: $the_ip was not found";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.