You can use a simple API from http://www.geoplugin.net/
$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=".getRealIpAddr());
echo $xml->geoplugin_countryName ;
echo "<pre>";
foreach ($xml as $key => $value)
{
echo $key , "= " , $value , " \n" ;
}
echo "</pre>";
Function Used
function getRealIpAddr()
{
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;
}
<?php
function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
$output = NULL;
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
$ip = $_SERVER["REMOTE_ADDR"];
if ($deep_detect) {
if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
}
$purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
$support = array("country", "countrycode", "state", "region", "city", "location", "address");
$continents = array(
"AF" => "Africa",
"AN" => "Antarctica",
"AS" => "Asia",
"EU" => "Europe",
"OC" => "Australia (Oceania)",
"NA" => "North America",
"SA" => "South America"
);
if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
switch ($purpose) {
case "location":
$output = array(
"city" => @$ipdat->geoplugin_city,
"state" => @$ipdat->geoplugin_regionName,
"country" => @$ipdat->geoplugin_countryName,
"country_code" => @$ipdat->geoplugin_countryCode,
"continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
"continent_code" => @$ipdat->geoplugin_continentCode
);
break;
case "address":
$address = array($ipdat->geoplugin_countryName);
if (@strlen($ipdat->geoplugin_regionName) >= 1)
$address[] = $ipdat->geoplugin_regionName;
if (@strlen($ipdat->geoplugin_city) >= 1)
$address[] = $ipdat->geoplugin_city;
$output = implode(", ", array_reverse($address));
break;
case "city":
$output = @$ipdat->geoplugin_city;
break;
case "state":
$output = @$ipdat->geoplugin_regionName;
break;
case "region":
$output = @$ipdat->geoplugin_regionName;
break;
case "country":
$output = @$ipdat->geoplugin_countryName;
break;
case "countrycode":
$output = @$ipdat->geoplugin_countryCode;
break;
}
}
}
return $output;
}
?>
Geolocation is a great way to put relevant content to your web visitors and it is a technology that can be used in almost any application you can think of.
In this tutorial, we use the IP2Location™ Web Service to lookup geolocation information from the visitor’s IP address. Instead of loading the full database, you can also lookup IP address via our hosted web service.
Below are the sample codes written in PHP to query and display user’s geolocation information.
- <?php
- $urlTemplate = 'http://api.ip2location.com/?' . 'ip=%s&key=demo' . '&package=WS24&format=json';
- $host= gethostname();
- $ipAddress = gethostbyname($host);
-
- // replace the "%s" with real IP address
- $urlToCall = sprintf( $urlTemplate, $ipAddress);
-
- $rawJson = file_get_contents( $urlToCall );
-
- $geoLocation = json_decode( $rawJson, true );
-
- if(isset($geoLocation['city_name'])){
-
- if($geoLocation['city_name']!="-"){
- echo '<script language="javascript">';
- echo 'alert("Welcome Visitors from '.$geoLocation['city_name'].'")';
- echo '</script>';
- }else
- {
- echo '<center>You are in local server!</center><br>';
- echo '<script language="javascript">';
- echo 'alert("You are in local server!")';
- echo '</script>';
- }
- }else{
- echo 'IP Address parsing error!';
- }
- ?>
- <html>
- <head>
- <title>IP2Location Web Service</title>
- </head>
- <body>
- <div>
- <center>Hello World!</center><br>
- </div>
- <div>
- <center>Your IP address <?php echo $ipAddress; ?></center>
- <center>
- <?php
- if(isset($geoLocation['country_code'])&&isset($geoLocation['country_name'])&&isset($geoLocation['region_name'])&&isset($geoLocation['city_name'])&&isset($geoLocation['latitude'])&&isset($geoLocation['longitude'])&&isset($geoLocation['zip_code'])&&isset($geoLocation['time_zone'])){
- echo '<br>Country Code:'."\n". $geoLocation['country_code'] . "\n<br>";
- echo 'Country Name:'."\n". $geoLocation['country_name'] . "\n<br>";
- echo 'Region Name:'."\n". $geoLocation['region_name'] . "\n<br>";
- echo 'City Name:'."\n". $geoLocation['city_name'] . "\n<br>";
- echo 'Latitude:'."\n". $geoLocation['latitude'] . "\n<br>";
- echo 'Longitude:'."\n". $geoLocation['longitude'] . "\n<br>";
- echo 'Zip code:'."\n". $geoLocation['zip_code'] . "\n<br>";
- echo 'Time zone:'."\n". $geoLocation['time_zone'] . "\n<br>";
- }else{
- echo 'IP Address parsing error!';
- }
- ?>
- </center>
- </div>
- </body>
- </html>
No comments:
Post a Comment