Over a million developers have joined DZone.

ip2country: How to get Country code and Country name by IP Address in PHP

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

We're going to use MaxMind's libraries

Download the following files and save them in a folder e.g. "geotest"
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz  and uncompress it.

Create a file in the same folder called e.g. geo.php

$ip = empty($_REQUEST['ip']) ? '' : $_REQUEST['ip'];

$gi = geoip_open("GeoIP.dat", GEOIP_STANDARD);
echo geoip_country_code_by_addr($gi, $ip);
Usage: http://yoursite.com/geotest/geo.php?ip=PUT_YOUR_IP_HERE

I use ipchicken.com to get my IP address.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of Svetoslav Marinov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}