IP2 Geolocation

File name : ip2locationlite.class.php

<?php
final class ip2location_lite{
protected $errors = array();
protected $service = ‘api.ipinfodb.com’;
protected $version = ‘v3’;
protected $apiKey = ”;

public function __construct(){}

public function __destruct(){}

public function setKey($key){
if(!empty($key)) $this->apiKey = $key;
}

public function getError(){
return implode(“\n”, $this->errors);
}

public function getCountry($host){
return $this->getResult($host, ‘ip-country’);
}

public function getCity($host){
return $this->getResult($host, ‘ip-city’);
}

private function getResult($host, $name){
$ip = @gethostbyname($host);

if(preg_match(‘/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/’, $ip)){
$xml = @file_get_contents(‘http://&#8217; . $this->service . ‘/’ . $this->version . ‘/’ . $name . ‘/?key=’ . $this->apiKey . ‘&ip=’ . $ip . ‘&format=xml’);
if (get_magic_quotes_runtime()){
$xml = stripslashes($xml);
}

try{
$response = @new SimpleXMLElement($xml);

foreach($response as $field=>$value){
$result[(string)$field] = (string)$value;
}

return $result;
}
catch(Exception $e){
$this->errors[] = $e->getMessage();
return;
}
}

$this->errors[] = ‘”‘ . $host . ‘” is not a valid IP address or hostname.’;
return;
}
}
?>

——————————————————————————–

File Name : result.php

<?php
include(‘ip2locationlite.class.php’);

$con = mysql_connect(“localhost”,”demoinfo_design”,”design4p”);
$dbc = mysql_select_db(“demoinfo_design4print”,$con);
if(!$dbc)
{
echo “Database doesn’t connect”;
}

//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey(‘202ee8e13a38c0424fe575e5657b46d2e34f981b3df8ad60e703f256f1b8d2aa’);

//Get errors and locations
$locations = $ipLite->getCity($_SERVER[‘REMOTE_ADDR’]);
//$locations = $ipLite->getCity(‘122.168.80.9’);
$errors = $ipLite->getError();

//Getting the result
/*
echo “<p>\n”;
echo “<strong>First result</strong><br />\n”;
if (!empty($locations) && is_array($locations)) {
foreach ($locations as $field => $val) {
echo $field . ‘ : ‘ . $val . “<br />\n”;
}
}
echo “</p>\n”;
*/
/*
echo “<pre>”;
print_r($locations);
echo “</pre>”;*/

$countryName = $locations[‘countryName’];
$countryCode = $locations[‘countryCode’];
$timeZone    = $locations[‘timeZone’];

//Show errors
/*
echo “<p>\n”;
echo “<strong>Dump of all errors</strong><br />\n”;
if (!empty($errors) && is_array($errors)) {
foreach ($errors as $error) {
echo var_dump($error) . “<br /><br />\n”;
}
} else {
echo “No errors” . “<br />\n”;
}
echo “</p>\n”;*/

————————————————————————————–

 Web based IP geolocation lookup

  Web based IP geolocation lookupIP Geolocation is a technique to identify the subject party’s IP address, then determines what country, state, city, ZIP Code, organization, or location the IP address has been assigned to. There are several IP geolocation providers in the industry supplying IP address databases. These databases contain IP address data which may be used in firewalls, domain name server, ad servers, routing, mail systems, web sites, and other automated systems where geolocation may be useful.
ip2locationlite.class.php
<?php
final class ip2location_lite{
protected $errors = array();
protected $service = ‘api.ipinfodb.com’;
protected $version = ‘v3’;
protected $apiKey = ”;public function __construct(){}

public function __destruct(){}

public function setKey($key){
if(!empty($key)) $this->apiKey = $key;
}

public function getError(){
return implode(“\n”, $this->errors);
}

public function getCountry($host){
return $this->getResult($host, ‘ip-country’);
}

public function getCity($host){
return $this->getResult($host, ‘ip-city’);
}

private function getResult($host, $name){
$ip = @gethostbyname($host);

if(preg_match(‘/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/’, $ip)){
$xml = @file_get_contents(‘http://&#8217; . $this->service . ‘/’ . $this->version . ‘/’ . $name . ‘/?key=’ . $this->apiKey . ‘&ip=’ . $ip . ‘&format=xml’);
if (get_magic_quotes_runtime()){
$xml = stripslashes($xml);
}

try{
$response = @new SimpleXMLElement($xml);

foreach($response as $field=>$value){
$result[(string)$field] = (string)$value;
}

return $result;
}
catch(Exception $e){
$this->errors[] = $e->getMessage();
return;
}
}

$this->errors[] = ‘”‘ . $host . ‘” is not a valid IP address or hostname.’;
return;
}
}
?>

result.php
<?php
include(‘ip2locationlite.class.php’);

//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey(‘API_KEY’);
//To get API_KEY register using this link http://ipinfodb.com/register.php
//Get errors and locations
//$locations = $ipLite->getCity($_SERVER[‘REMOTE_ADDR’]);
$errors = $ipLite->getError();

//Getting the result
echo “<pre>”;
print_r($locations);
echo “</pre>”;
?>

After finished the setup run the result.php will get country value.

—————————————————————————

IP2 Geo Location :<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>GeoLocation</title>
</head>
<body>
<?php
function ip_address_to_number($IPaddress) {
if ($IPaddress == ”) {
return 0;
} else {
$ips = explode(‘.’, $IPaddress);
return ($ips[3] + $ips[2] * 256 + $ips[1] * 65536 + $ips[0] * 16777216);
}
}

$IPaddress  = $_SERVER[‘REMOTE_ADDR’];
echo ip_address_to_number($IPaddress);
echo “<br />”;
echo “<br />”;
echo “<br />”;

echo “<pre>”;
echo var_export(unserialize(file_get_contents(‘http://www.geoplugin.net/php.gp?ip=&#8217;.$_SERVER[‘REMOTE_ADDR’])));
echo “</pre>”;
?>
</body>
</html>