Win Unix Mac

Articles,How Tos,Tips n more

  • Increase font size
  • Default font size
  • Decrease font size

PHP - Geo IP example setup using Fedora 11

E-mail Print PDF

The PECL Geo IP PHP extension is easy to configure and simple to use. Add it to your website to control and enhance it adding proximity and geo location functionalities. In this example I show how to lookup a hostname and extract the details of the city, country name and country code.

Firstly you need an existing php / apache webserver setup, this guide only covers adding the PECL and Geo IP packages to the system.

Make sure you have these packages installed:

php-pecl-geoip-1.0.7-2.fc11.i586
GeoIP-1.4.6-1.fc11.i586

If they are not already installed, use yum (as root or using sudo !) to install them (see that mine are already installed, but there is an updated GeoIP package)

[root@f11 GeoIP]# yum install php-pecl-geoip GeoIP
Loaded plugins: refresh-packagekit
Setting up Install Process
Package php-pecl-geoip-1.0.7-2.fc11.i586 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package GeoIP.i586 0:1.4.6-2.fc11 set to be updated
--> Finished Dependency Resolution

If you have just installed or updated your PECL and Geo IP packages, you need to stop and start apache:

[root@f11 GeoIP]# service httpd stop

Stopping httpd:                                            [  OK  ]

[root@f11 GeoIP]# service httpd start

Starting httpd:                                            [  OK  ]


Now you need to add the Free / Lite version of the city lookup db. Full details at the MaxMind website. Make sure to place and unzip the file in /usr/share/GeoIP/

[root@f11 GeoIP]# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

[root@f11 GeoIP]# gunzip GeoLiteCity.dat.gz

And finally just link to the CityIP name (mine already exists):

[root@f11 GeoIP]# ls -l GeoIPCity.dat

lrwxrwxrwx 1 root root 15 2009-11-04 22:47 GeoIPCity.dat -> GeoLiteCity.dat

[root@f11 GeoIP]# ln -s GeoLiteCity.dat GeoIPCity.dat

ln: creating symbolic link `GeoIPCity.dat': File exists


See that was not difficult at all ! Here is some example code to use for the function geoip_record_by_name:

[root@f11 GeoIP]# cat /var/www/html/testgeo.php


<?

$domain="winunixmac.com";

$visitor=geoip_record_by_name($domain);

$city=$visitor["city"];

$country=$visitor["country_name"];

$country_code=$visitor["country_code"];

print "Website $domain is hosted in $city, $country ($country_code)\n";

?>

The output from the Geo IP sample code:

[root@f11 GeoIP]# php /var/www/html/testgeo.php

Website winunixmac.com is hosted in Scottsdale, United States (US)

Last Updated on Wednesday, 04 November 2009 23:02  
Comments (1)
Nice, the one i use
1 Wednesday, 10 March 2010 14:18
aminoacid
Nice article man!
Just like to point out there is a nice mashup of google maps with Geolocation (Ip to Country) which i use often here:
http://www.astalavista.com/index.php?app=onlinetools&module=ipinfo

Add your comment

Your name:
Subject:
Comment:

yvcomment, category: "Unix How To's"