How to use MaxMind GeoIP in PHP?

I am using the binary version of MaxMind's GEOIPCITY database. The following code gives me all the information I need about my visitors:


$gi = geoip_open("GeoLiteCity.dat", GEOIP_STANDARD);
print_r( geoip_db_get_all_info() );
$record = geoip_record_by_addr($gi, $user_ip);
print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "<br /><br />";
print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "<br /><br />";
print $record->city . "<br /><br />";
print $record->postal_code . "<br /><br />";
print $record->latitude . "<br /><br />";
print $record->longitude . "<br /><br />";
print $record->metro_code . "<br /><br />";
print $record->area_code . "<br /><br />";
print $record->continent_code . "<br /><br />";

I don't really need anything beyond this like network speed, ISP, etc.

My issue is, there are parts of my site where I need to display all the cities under a particular state, or all the states/regions in a particular country. For example I need to do this in the registration form.

Is there no way to get these lists by querying the binary file? or do I have to handle all that from the mysql version?

I would prefer to use only the binary version since it's faster, but now I am not sure if the displaying of all zips under a certain city or all states under a certain country, etc. is possible without using the mysql version. Does one need to use both?


The GeoIP database is intended for one purpose, and one purpose only: to return location information for an input IP. It is not a general-purpose GIS database, and will not work for that purpose, as there are some locations which won't be returned for any IP. (For instance, a small town with no local Internet service might not appear in their database at all, since any IP in that town would likely be categorized under another nearby city.)

Maxmind has a separate "World Cities with Population" database that is more likely to be suitable for this purpose:

I just wrote a linux daemon to serve MaxMind city-level geoip queries. Each query takes about 3 microseconds and while it's running, the server takes up about 550MB of ram on a 64bit machine (and 300MB on a 32bit machine).

I'm sure you can modify it to index states as well. Let me know if you need help and I'll gladly help you modify it.

Hope that helps...

Need Your Help

Problems with C# lists, and ArgumentOutOfRangeException

c# list xna outofrangeexception

I am in the process of making a farming/tower defense game and I am very new at programming. I seem to have a major problem with using Lists&lt;> or arrays in XNA. I cannot get it to return the index

Fetch current user through store.find('users', 'me') stores invalid User instance

javascript ember.js ember-data

I am trying this solution to fetch the current user before my application is rendered to the user:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.