Home

See the related posts

Mysql server version using php function – mysql_get_server_info()    Text file prompt download or force download using php    Number of lines in a file using php    Check Image exists or Not Using php and GD    PHP Warning: Call-time pass-by-reference has been deprecated   

PHP IP Geographic Location Info

You can use this code for display the loaction details of a perticluar ip address or a Server or a website.
This code is taken form www.ipinfodb.com and modified for better results. Just include the following class and create an object and get the corresponding values.

<?php

class IPDetails{
	var $ip;
	var $countryName;
	var $countryCode;
	var $status;
	var $regionCode;
	var $regionName;
	var $city;
	var $zip;
	var $latitude;
	var $longitude;
	var $gmtOffset;
	var $dstOffset;
	var $timezone;

	function collectIPDetails($ip){
		//$proxy = "74.125.45.100";
		$addr = "http://www.ipinfodb.com/ip_query.php?ip=".$ip;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $addr);
		//curl_setopt($ch, CURLOPT_PROXY, $proxy);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_HTTPGET,1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
		curl_setopt($ch, CURLOPT_TIMEOUT,10);
		$pg = curl_exec($ch);
		//echo $pg;
		if($pg){
			$data = new SimpleXMLElement($pg);
			$this->ip = $data->Ip;
			$this->countryName = $data->CountryName;
			$this->countryCode = $data->CountryCode;
			$this->status = $data->Status;
			$this->regionCode = $data->RegionCode;
			$this->regionName = $data->RegionName;
			$this->city = $data->City;
			$this->zip = $data->ZipPostalCode;
			$this->latitude = $data->Latitude;
			$this->timezone= $data->Timezone;
			$this->longitude = $data->Longitude;
			$this->gmtOffset = $data->Gmtoffset;
			$this->dstOffset = $data->Dstoffset;
			}
	}
}// class ends
?>

<?php 

	$dt=new IPDetails();

	//if(isset($_REQUEST['ip'])){
	$ips = gethostbynamel($_REQUEST['ip']);

echo '<br><br><table border="0">';
echo '<tr><td width="150" align="center" bgcolor="#A3EBFF">Server Name</td><td width="" align="center" bgcolor="#A3EBFF">IP Address</td><td width="150px" align="center" bgcolor="#A3EBFF">Country</td><td width="" align="center" bgcolor="#A3EBFF">Region</td><td width="100" align="center" bgcolor="#A3EBFF">City</td><td width="" align="center" bgcolor="#A3EBFF">ZipCode</td><td width="" align="center" bgcolor="#A3EBFF">Timezone</td><td width="" align="center" bgcolor="#A3EBFF">Latitude</td></tr>';

 if(is_array($ips)){
	    foreach($ips as $ip ){
 		$dt->collectIPDetails($ip);
		$img="http://www.selfseo.com/gfx/flags/".strtolower($dt->countryCode).".png";
		echo '<tr><td align="center" bgcolor="#DCEFFF">'.gethostbyaddr($ip).'</td>';
		echo '<td align="center" bgcolor="#DCEFFF">'.$ip.'</td>';
		echo '<td align="center" bgcolor="#DCEFFF">'.$dt->countryName.'&nbsp;<img src="'.$img.'"></td>';
		echo '<td align="center" bgcolor="#DCEFFF">'.$dt->regionName.'</td>';
		echo '<td align="center" bgcolor="#DCEFFF">'.$dt->city.'</td>';
		echo '<td align="center" bgcolor="#DCEFFF">'.$dt->zip.'</td>';
		echo '<td align="center" bgcolor="#DCEFFF">'.$dt->timezone.'</td>';
		echo '<td align="center" bgcolor="#DCEFFF">('.$dt->latitude.' , '.$dt->longitude.')</td>';
		echo '</tr>';
		}
	echo '</tr></table><br>';
  }
}

?>

You can view a demo for this codes from View Demo
Postt Ur comments and queries…. :)

Home

See the related posts

Mysql server version using php function – mysql_get_server_info()    Text file prompt download or force download using php    Number of lines in a file using php    Check Image exists or Not Using php and GD    PHP Warning: Call-time pass-by-reference has been deprecated   

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment