Technology Blog

Partners

WhichIsBest

Ads

ads
WhichIsBest

Friday, October 7, 2011

Find Real IP address and location details of visitor


Find Real IP address and location details of visitor


Finding real IP address of a user is becoming highly important now-a-days, that too if the visitor is behind any firewall it's getting as a tough task for the webmasters to trace the real ip address of the visitor. Mostly all the webmasters make use of Google Analytics to trace their visitors, where in the IP address is masked. So, here i come with a solution with the help of the following

Language : PHP with CURL
IP API : IPinfoDB.com JSON API
Map API : Google Chart API


About  IPInfoDB.com

IPInfoDB offer a wide range of free services based on our IP address geolocation database :

  • Web based IP geolocation lookup

  • IP geolocation API (XML, JSON and CSV format)

  • IP database updated monthly!

To find the IP address of the user you will need to make use of the IPInfoDB.com API in this tutorial, so register yourself for a FREEaccount.

Register at IPInfoDB.com to get the API Key


About Google Chart

Google Chart Tools provide a perfect way to visualize data on your website. We will be using Visualization: Geochart.

realip_details.php



<?php
/**
* GetIpDetails
* Author : S.MohanKumar
* IP API from ipinfodb.com & Google Chart API
**/
$realIp='';
function GetIpDetails($realIp){
//check if you have curl loaded
if(!function_exists("curl_init")) die("cURL extension is not installed");
// create a new cURL resource
$ch = curl_init();
//ipinfodb.com API Key
$ipinfodbApiKey='YOUR_FREE_API_KEY_FROM_IPINFODB.com';

// set URL and other appropriate options
$url="http://api.ipinfodb.com/v3/ip-city/?key=".$ipinfodbApiKey."&format=json&ip=".$realIp;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, false);

// grab URL and pass it to the browser
$ipDetails_json=curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

return $ipDetails_json;
$obj = json_decode($ipDetails_json);
}

function getRealIpAddress()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
//check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
//to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}

getRealIpAddress(); // display the real IP address
echo "<br/>";
$openIp=getRealIpAddress($realIp);
$ipDetails_json =GetIpDetails($openIp);
$obj = json_decode($ipDetails_json);
?>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages': ['geochart']});
google.setOnLoadCallback(drawRegionsMap);

function drawRegionsMap() {
var data = new google.visualization.DataTable();
data.addRows(1);
data.addColumn('string', 'Country');
data.addColumn('string', 'Title');
data.setValue(0, 0, '<?php print $obj->{'countryName'}; ?>');
data.setValue(0, 1, 'IP : <?php print $obj->{'ipAddress'}." ".$obj->{'cityName'}.",".$obj->{'regionName'}.",".$obj->{'countryName'}; ?>');

var options = {};

var container = document.getElementById('map_canvas');
var geochart = new google.visualization.GeoChart(container);
geochart.draw(data, options);
};
</script>
<div id='map_canvas' style="width:600px"></div>

<fieldset>
<legend><b>Ip Details </b></legend>
<?php
echo "<b>IPaddress :</b> ".$obj->{'ipAddress'}."<br/>"; // IpAddress
echo "<b>Country name :</b> ".$obj->{'countryName'}."<br/>"; // countryName
echo "<b>State name :</b> ".$obj->{'regionName'}."<br/>"; // regionName
echo "<b>City name :</b> ".$obj->{'cityName'}."<br/>"; // cityName
echo "<b>Latitude :</b> ".$obj->{'latitude'}."<br/>"; // latitude
echo "<b>Longitude :</b> ".$obj->{'longitude'}."<br/>"; // longitude
echo "<b>Time zone :</b> ".$obj->{'timeZone'}."<br/>"; // timeZone
?>
</fieldset>



DOWNLOAD

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice blog the script is helpful for those who dont knoe anything about how to find the ip address of visitors ..I used the script which you mentioned here and easily find the ip address of my website visitors .After finding the ip address i easily traced his location just by doing whois search for that ip address using sites like WhoisXY.com here they will display details like ip location ,ISP name ,address ,email id and so on but i can't trace exact home address of any person from ip ..I can trace only ISP details from ip address ..

    ReplyDelete

WhichIsBest
WhichIsBest