troubleshooting Question

Any GeoIP DB users? Need help converting I.P. address to integer.

Avatar of mcgeorge40
mcgeorge40 asked on
PHP
40 Comments1 Solution757 ViewsLast Modified:
I am TRYING to use the MaxMind DB (available at MaxMind.com), but their documentation kind of sucks.  If you couple that with the fact that I am teaching myself SQL as I go, I am ready to rip out my hair.

I need to use PHP to convert an I.P. address into an integer, so that I can use a "greater than / less than" thing on them.

There are two tables in the database.  One is called "blocks".  Blocks contains 3 fields: a start_IP, an end_IP and a location_ID.

The MAX mind documentation says the table contains 6 fields, but it doesn't.  It is now broken up into two tables.

For an experienced programmer, it might only take a second to look at http://www.maxmind.com/app/csv

...and determine how to convert the I.P.

Bottom line:  I need the PHP code necessary to take an I.P. address and convert it to an integer.  It's NOT as simple as removing the dots.

There is some help at http://www.maxmind.com/app/csv

It says:  
--------------------------------------------
 Beginning IP Number and Ending IP Number are calculated as follows:

ipnum = 16777216*w + 65536*x + 256*y + z   (1)

where

IP Address = w.x.y.z

The reverse of this formula is

w = int ( ipnum / 16777216 ) % 256;
x = int ( ipnum / 65536    ) % 256;
y = int ( ipnum / 256      ) % 256;
z = int ( ipnum            ) % 256;

Where % is the mod operator.
-----------------------------------------------

This all means nothing to me.  All I know is that I have to calculate the "ending IP number", since that is the primary key in the database.  With that, I can get the location ID for a particular city / country.

I know this is probably a lot simpler than it looks, but I am losing it.  I thank you all in advance!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 40 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 40 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros