Converting IP number to an IP address

A bit of a random one, this.

I’m trying to set up some basic Geo Targeting one of my sites. I have a database populated with Beginning and Ending IP numbers and the relevant countries.

What I’m trying to do is write a PHP script which will take a user’s IP address and the convert that into IP numbers in order for me to tell which country the user is from.

Can anyone point me in the rough direction of a blueprint that might help me out with this? I guess I just need to get an idea of the best way of going about this.

Thanks a lot for your help,

That is what I’m using - but I’ve had to their CSV files to import the data into an SQL database rather than installing their binary databases.

Or am I over complicating things?

Can’t see why you don’t just use GEOIP (see RH column)

Ok here’s an example of the way that the SQL database is structured:

"begin_ip","end_ip","begin_num","end_num","country","name"
"61.88.0.0","61.91.255.255","1029177344","1029439487","AU","Australia"
"61.92.0.0","61.93.255.255","1029439488","1029570559","HK","Hong Kong"
"61.94.0.0","61.94.7.255","1029570560","1029572607","ID","Indonesia"

What I need to do is take a user’s IP address and match it up to a particular country in the database.

Now my understanding of the situation is that I need to convert a user’s IP address and convert it into an IP number to compare it to the “begin_num” and “end_num” columns shown above.

Is that correct? Or am I over complicating things?

I don’t understand your question. IP addresses are already numbers. Just use:

$ip=$_SERVER['REMOTE_ADDR'];

Sorry to keep pushing this instead of helping you with the solution that you seem to have set your mind on. The “binary database” is just an ordinary file like any other. As long as your server allows the fopen/fread family of functions, you can use it. The plain-PHP API is available on PEAR as Net_GeoIP.

See INET_ATON(). :slight_smile:

Well I never.

I just love it how when I throw caution to the wind and risk making an *ss of myself in public - I learn something.

Although, I admit I’m doing a lot of this lately in other fields of endeavour too.

Yes, they are Mike.

Don’t apologise - I appreciate your help.

So, if I go down the binary database route, is it just a case of uploading the relevant .dat and .inc files on to the server? I was under the impression that I had to run several linux commands to install these files properly, which obviously wasn’t possible on a shared server.

“61.88.0.0”,“61.91.255.255” [thinks]

how would it be if there were 2 indexes in the table which contained a reduced IP range in the format;

61088, 61091

That would have to presume that the 2 digit sets “0.0” and “255.255” are redundant.

In my 2 examples “127.0.0.1” or “61.90.123.123” i’d have to reduce them to 127000 and 61090.

The first is clearly greater than both, and the latter should get caught.


<?php
$sql = sprintf(
  "SELECT country FROM table WHERE begin_num >= INET_ATON('&#37;$1s') AND end_num <= INET_ATON('%$1s');",
  mysql_escape_string($_SERVER['REMOTE_ADDR'])
);
?>

See INET_ATON(). :slight_smile:

So, if I understand, your question is then: how do I detect whether the likes of “127.0.0.1” or “61.90.123.123” fall within the range:

“61.88.0.0”,“61.91.255.255”

The problem is though that I’ve had to use the CSV version of Maximind’s data and import that into an SQL database.

And you’d like to do that filtering in an SQL statement?

(Sounds as if this is going to be sticky)

Oh right, well I’ll certainly give it a go this evening.

I think I was just put off by these instructions on the MaxMind

First, download the database, and transfer the file to your server. Once you have uploaded the GeoIP tarball to your server, open a ssh session and run the following commands:

$ tar xvfz GeoIP-106_20051201.tar.gz
$ mv GeoIP-106_20051201/GeoIP-106_20051201.dat /usr/local/share/GeoIP/GeoIP.dat

Is it ok to ignore that?

Because my hosting is currently set up on a shared server, and so therefore I don’t have the permissions to install a binary database on it.

I think Sterling has the answer. I was going to suggest similar using BETWEEN in the SQL and ip2long($_SERVER[‘REMOTE_ADDR’]), but I see MySQL has it’s own equivalent.

Simon are the begin_num and end_num columns numeric? They should be.

Whilst it wasn’t intended to be a response to you Cups, I’ll take the credit nonetheless. :smiley:

I’ve said it before though, I do like seeing/reading how you work things out.

Here’s hoping it provides the solution the OP is looking for.

Anthony.

You keep saying that you need to use a SQL database, can you explain why?

Using Maxmind’s goodies means everything can live on your server; YQL, not so much the case. The “pure PHP” api is really nice and simple to use, and will save you cooking up your own functions to do the same job.

are you trying to convert IP’s to integers for smaller storage?

then ip2long() is your answer - http://php.net/ip2long

I dev on a PC but live server is *nix, and to the best of my memory I downloaded the windows version of .dat to my pc and SFTP’d it onto the server, 'cause I find many *nix commands to be scary too, I am getting over it though.