Subscribe via RSS

PHP Geo IP Address Lookup

Share and Enjoy:
  • Print
  • email
  • Digg
  • DZone
  • Twitter
  • Facebook
  • del.icio.us
  • Google Bookmarks
  • Reddit
  • Slashdot
  • Technorati
  • Yahoo! Buzz

Today I searched on how to get the city of a user based on their ip address. I was looking to do this using PHP and preferably for free. There was quiet a bit out there that had a free database you could download to cross reference that would return the country. However, we wanted to be able to get the city and state as well. After quiet a bit of googling, I came across a free API from ipgp.net. They allow you to post the ip to a URL and they return an XML packet with the county, a path to the image of the country’s flag, city, state, isp, latitude, and longitude. They have an example on how to do this on their page. We created our own class below for your use that is a little cleaner. It can be included in your php scripts and you don’t have to worry about downloading anything into MySQL.

	/*
		Provided by http://www.todayisearched.com
	*/
	class GeoIP {

		/*
			Returns all possible Geo Ip information in an associative array.  The following pieces of information
			are returned:
			IP - IP ADDRESS LOOKED UP
			CODE - COUNTRY CODE
			COUNTY - COUNTRY NAME
			FLAG - PATH TO IMAGE OF THE COUNTRY'S FLAG
			CITY - CITY NAME
			REGION - STATE NAME
			ISP - ISP NAME
			LAT - LATITUDE CORDINATE
			LNG - LONGITUDE CORDINATE

			USAGE:
			$ipinfo = GeoIP :: getGeoArray('xxx.xxx.xxx.xxx');
			echo $ipinfo['CITY'] . ', ' . $ipinfo['STATE'];
		*/
		public static function getGeoArray($ip) {
			$file = "http://www.ipgp.net/api/xml/". $ip;

			$xml_parser = xml_parser_create();
			$fp = fopen($file, "r");
			$data = fread($fp, 80000);
			xml_parse_into_struct($xml_parser, $data, $vals);
			$iplookup = array();
			foreach ($vals as $v) {
				if (isset($v['tag']) && isset($v['value'])) {
					$iplookup[$v['tag']] = $v['value'];
				}
			}
			xml_parser_free($xml_parser);
			fclose($fp);

			return $iplookup;
		}

		//shortcut to get the city name
		public static function getCity($ip) {
			$a =  self :: getGeoArray($ip);
			return $a['CITY'];
		}

		//shortcut to get the state name
		public static function getState($ip) {
			$a =  self :: getGeoArray($ip);
			return $a['REGION'];
		}

	}

Follow these steps to implement the above script:

  1. Copy the script above and save it into a php file on your server
  2. Add include_once(‘Path To The PHP File Saved On #1′);
  3. Use the code snippet below in your php to obtain geoip information.
      include_once "geoip.php";
      $ipinfo = GeoIP :: getGeoArray($_SERVER['REMOTE_ADDR']);
      echo 'Hello, Your City And State Is ' . $ipinfo['CITY'] . ', ' . $ipinfo['REGION'];

Thanks http://www.ipgp.net for providing this invaluable service!

Share and Enjoy:
  • Print
  • email
  • Digg
  • DZone
  • Twitter
  • Facebook
  • del.icio.us
  • Google Bookmarks
  • Reddit
  • Slashdot
  • Technorati
  • Yahoo! Buzz

One Response to “PHP Geo IP Address Lookup”

  • Thanks! This is great! I’ve been searching for a free API that returns GeoIP data, and this is the first free one that returns tiny piece of data (XML) and really returns the City. AWESOME!!!