Making GeoIP - based IP locator with Maps and twitter post in PHP

Twittergeolocation

Almost everybody curious about location , and today is the "Location Based" era when people *i mean not only the geek* become crazy with it . Let say koprol and foursquare , these are the location based social media that using this "craziness" become their main service. Also it is easier for the service provider to be more local by identifying their users , e.g : when we access the http://www.google.com from Indonesia , it will redirect us to http://www.google.co.id by default and there , we can see the interface of Google in Indonesian ( Bahasa Indonesia ) .

How could it be ?? simple , they detect our IP (Internet Protocol) and match it to their database to know the location of the users . Information may vary , from only the Country until the exact location of the users. For now , we will talk about making a Geo Location service using GeoIP from maxmind in PHP.

What are the capabilities of the software we will make for now ??

1. Detect the IP

2. Locate it and get the additional info (country , city , lat , long , ISP , zipcode etc)

3. Show it on Map

4. Post the result to twitter to see the log so we don't have to make database for it :-D

For the demo , you can see at : http://iploc.co.cc

OK , let see the code :

Click here to download:
iploc.doc (28 KB)

Let's Extract the code :

1. To get the IP :

if (isset($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']) && eregi("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$", 

 $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'])){

$ip = $HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR'];

}

else{

$ip = getenv("REMOTE_ADDR");

}

2. The GeoIP information :

we need at least three libraries here :

include("geoip.inc");

include("geoipcity.inc");

include("geoipregionvars.php");

Get the PHP libraries here : http://geolite.maxmind.com/download/geoip/api/php/

and two free maxmind GeoIP info database ( dat or CSV ), but i have three :-p another one is for the ISP.

$gic = geoip_open("country.dat",GEOIP_STANDARD);

$gi = geoip_open("city.dat",GEOIP_STANDARD);

$giisp = geoip_open("isp.dat",GEOIP_STANDARD);

GeoIP databases can be downloaded here ( not included the ISP database ) :

http://geolite.maxmind.com/download/geoip/database/

and the flag images here :

http://iploc.co.cc/flags/

3. Show it on Map :

we use 4 Google Maps here , country , region , city , and the latitude longitude place (not the exact place , we will discuss it in the next post) . You have to generate the key for each map at http://code.google.com/apis/maps/signup.html

4. Post it to twitter : 

By using twitter class for php (basic Auth will be deprecated on June) , in this example is to simplify the implementation .

include("Twitter.class.php");

$twitter = new Twitter();

$twitter->username = "twitter_username";

$twitter->password = "twitter_password";

$twitter->update('xml','IP : '. $ip .' '. $isp .','. $record->country_name .','. $GEOIP_REGION_NAME[$record->country_code][$record->region] .','. $record->city .', ('. $record->latitude .','. $record->longitude.') #iploc');

Full code of Twitter.class.php is here :

Click here to download:
Twitter.class.doc (31 KB)

See it in action : 

  • Wireless Broadband (im2):

Iploc1

  • Wired Connection (i don't have one , so i used proxy that i put on my "private" server somewhere in Bandung :-p ) :

Iploc2

  •  Post on twitter :

Iploc3

OK , That's it.. i hope you like it .. Thanks

Panggi Libersa Jasri Akadol

Panggi Libersa Jasri Akadol

I am an open minded person, totally platform agnostic and believe in using the right tool for the right job

Subscribe...

Follow via RSS

Archive

2011 (12)
2010 (56)

Contributors