IP to location

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

IP to location

Post by thosecars82 »

Hello
I am trying to translate IP addressses into locations.
To begin with, I was looking for a good script to translate an IP address to city and using this script in a website X. I have not found any reliable one yet though.
Moreover, I just found a few websites which might be reliable for this purpose.

On one hand, since these websites do not make their code available I had thought of doing an http request to any of these websites, let's call it Y, in order to get the address corresponding to the IP.

On the other hand, from the point of view of the delay involved, I am not sure whether it would be a good idea this http request to a website Y in order to get content which would be used to display website X.

What do you recommend?
Thanks.
Last edited by thosecars82 on Wed Apr 06, 2011 3:02 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IP to location

Post by John Cartwright »

MaxMind has some free geoIp2City databases for download.

And no, it's not a good idea to make external requests on every load.
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

Re: IP to location

Post by thosecars82 »

John Cartwright wrote:MaxMind has some free geoIp2City databases for download.

And no, it's not a good idea to make external requests on every load.
Thanks for the advices.
Is that freeware?
I just forgot to mention that I was looking for the most reliable freeware option.
Last edited by thosecars82 on Thu Oct 14, 2010 4:41 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IP to location

Post by John Cartwright »

thosecars82 wrote:Is that freeware?
I just forgot to mention that I was looking for the most reliable freeware option.
Like I said, free geoIp2City database

See http://www.maxmind.com/app/geolitecity

When it comes to GeoIP and free, you have to throw accuracy out the window. They generally do not update their databases frequently, and by nature IP addresses change very frequently (as IP blocks are sold, shifting, etc., sometimes to completely different continents). I don't know what they mean by realibility, you I assume you meant accuracy, and in that case I already touched on that.
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

Re: IP to location

Post by thosecars82 »

John Cartwright wrote:
thosecars82 wrote:Is that freeware?
I just forgot to mention that I was looking for the most reliable freeware option.
Like I said, free geoIp2City database

See http://www.maxmind.com/app/geolitecity

When it comes to GeoIP and free, you have to throw accuracy out the window. They generally do not update their databases frequently, and by nature IP addresses change very frequently (as IP blocks are sold, shifting, etc., sometimes to completely different continents). I don't know what they mean by realibility, you I assume you meant accuracy, and in that case I already touched on that.
Thanks for the info.
Yeah, accuracy is what I meant, and that is what I should have said from the beginning.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IP to location

Post by John Cartwright »

I guess I really didn't answer your question. But yes, MaxMind is probably the best "freeware" geoIP out there (beyond external services).
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

Re: IP to location

Post by thosecars82 »

John Cartwright wrote:I guess I really didn't answer your question. But yes, MaxMind is probably the best "freeware" geoIP out there (beyond external services).
Thanks. I believe you did answer my question pretty well. Now I gotta test it.
Andyou are right, first time you mentioned GeoLite City you said it was free.
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

Re: IP to location

Post by thosecars82 »

Can this GeoIP C API from http://www.maxmind.com/app/c work in Windows instead of Linux? If that is the case, how could it be installed in Windows?

I just saw in http://www.maxmind.com/app/php that to make this work in php, this GeoIP C API must be installed.
Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IP to location

Post by John Cartwright »

Please read the docs more carefully before asking questions.

You are looking at the pure PHP module. It involves simply placing the text file somewhere in your filesystem, and using the functions provided to interface with the database. This option does not involve any extensions or installations.

They do however offer PHP extensions and Apache modules for faster performance, but thats beyond what I'm willing to help you debug (just use their documentation).
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

Re: IP to location

Post by thosecars82 »

Sorry for asking something that was written on the website.
Another question: do you know whether this GeoLite City is useful all over the world. I just asked because I saw the US word mentioned in the following paragraph. I still think it must be usefull no matter the IP's country except for the postal code and area code fields which mightl only work in the US.
"Product Summary: Determine country, state/region, city, US postal code, US area code, metro code, latitude, and longitude information for IP addresses worldwide. The downloadable database is available to established businesses only, please see the availability section below for details."

It seems as if it might only work for the US.

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IP to location

Post by John Cartwright »

From my experiece, it works beyond North America, but the accuracy isn't all that great.
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

Re: IP to location

Post by thosecars82 »

I'm getting this warning
Warning: fopen(./GeoIP.dat) [function.fopen]: failed to open stream: the file or the directory does not exist in ...
Any idea?
The code is

Code: Select all

<?php
include("geoip.inc");
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
and is being used inside of wordpress.
Thanks
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

Re: IP to location [SOLVED]

Post by thosecars82 »

thosecars82 wrote:I'm getting this warning
Warning: fopen(./GeoIP.dat) [function.fopen]: failed to open stream: the file or the directory does not exist in ...
Any idea?
The code is

Code: Select all

<?php
include("geoip.inc");
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
and is being used inside of wordpress.
Thanks
Solved. I just had to use getCWD() in order to set the proper path when calling fopen.
Thanks
thosecars82
Forum Commoner
Posts: 94
Joined: Thu Apr 03, 2008 6:31 am
Location: Arganda, Madrid
Contact:

Re: IP to location

Post by thosecars82 »

I got another question.
I got different results for the same ip when I used my script (GeoLite) in contrast with http://phpweby.com/services/iplocation which should give the same result becuase this website states they also use the same database GeoLite.
Do you know why I might be getting different results?
Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IP to location

Post by John Cartwright »

John Cartwright wrote:When it comes to GeoIP and free, you have to throw accuracy out the window. They generally do not update their databases frequently, and by nature IP addresses change very frequently (as IP blocks are sold, shifting, etc., sometimes to completely different continents). I don't know what they mean by realibility, you I assume you meant accuracy, and in that case I already touched on that.
Post Reply