user967451
user967451

Reputation:

How to get a user's location based on their IP address?

To get the user's IP I'm using:

$ip = $_SERVER['REMOTE_ADDR'];

I have a geo database with the following tables:

blocks
locations
zipcodes
nations
dma_codes
states

This is the table I'm interested in

blocks

start_ip | end_ip | location_id
-------------------------------

33996344 | 33996351 | 16373
50331648 | 67166463 | 223

etc...

Above are just the first 2 records of the 1,866,401 rows in total in that table. I have no idea which geo database this is. Maybye MaxMind but not sure. I inherited this project from the client's previous developer who uploaded this database but didn't do anything with it yet. So I have no code samples to go on.

Can anyone please explain how I would get the location_id if my ip address happens to be:

72.63.83.41

Also does anyone recognize these table names so maybye I can find out which geo db this is.

Upvotes: 0

Views: 1279

Answers (1)

Marc B
Marc B

Reputation: 360882

IPv4 addresses are just 32bit integers, and you can use MySQL to convert a 'string' IP address to/from those ints via inet_ntoa() (number to ascii) and inet_aton() (ascii to number).

SELECT location_id
FROM yourtable
WHERE INET_ATON('70.63.83.41') BETWEEN start_ip AND end_ip

would give you any matching location_id for that particular IP.

Upvotes: 2

Related Questions