Get location using IP address in PHP

This tutorial specially for webmasters who want to trace visitor's location from IP address.
we can create this easily using geoPlugin API. We can extract users location information like city, region, areaCode, dmaCode, countryCode, countryName, continentCode, latitude, longitude, currencyCode, currencySymbol, currencyConverter from IP address. Let's see how we can create this program. First, create a geoPlugin.php file and write down this code.

geoPlugin.php 

<?php


class geoPlugin {

var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}';
var $currency = 'USD';
var $ip = null;
var $city = null;
var $region = null;
var $areaCode = null;
var $dmaCode = null;
var $countryCode = null;
var $countryName = null;
var $continentCode = null;
var $latitude = null;
var $longitude = null;
var $currencyCode = null;
var $currencySymbol = null;
var $currencyConverter = null;

function geoPlugin() {

}

function locate($ip = null) {

global $_SERVER;

if ( is_null( $ip ) ) {
$ip = $_GET["ip"];

}

$host = str_replace( '{IP}', $ip, $this->host );
$host = str_replace( '{CURRENCY}', $this->currency, $host );

$data = array();

$response = $this->fetch($host);

$data = unserialize($response);

//set the geoPlugin vars
$this->ip = $ip;
$this->city = $data['geoplugin_city'];
$this->region = $data['geoplugin_region'];
$this->areaCode = $data['geoplugin_areaCode'];
$this->dmaCode = $data['geoplugin_dmaCode'];
$this->countryCode = $data['geoplugin_countryCode'];
$this->countryName = $data['geoplugin_countryName'];
$this->continentCode = $data['geoplugin_continentCode'];
$this->latitude = $data['geoplugin_latitude'];
$this->longitude = $data['geoplugin_longitude'];
$this->currencyCode = $data['geoplugin_currencyCode'];
$this->currencySymbol = $data['geoplugin_currencySymbol'];
$this->currencyConverter = $data['geoplugin_currencyConverter'];

}

function fetch($host) {

if ( function_exists('curl_init') ) {

//use cURL to fetch data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0');
$response = curl_exec($ch);
curl_close ($ch);

} else if ( ini_get('allow_url_fopen') ) {

//fall back to fopen()
$response = file_get_contents($host, 'r');

} else {

trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR);
return;

}

return $response;
}

function convert($amount, $float=2, $symbol=true) {

//easily convert amounts to geolocated currency.
if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
return $amount;
}
if ( !is_numeric($amount) ) {
trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
return $amount;
}
if ( $symbol === true ) {
return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
} else {
return round( ($amount * $this->currencyConverter), $float );
}
}

function nearby($radius=10, $limit=null) {

if ( !is_numeric($this->latitude) || !is_numeric($this->longitude) ) {
trigger_error ('geoPlugin class Warning: Incorrect latitude or longitude values.', E_USER_NOTICE);
return array( array() );
}

$host = "http://www.geoplugin.net/extras/nearby.gp?lat="; . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}";

if ( is_numeric($limit) )
$host .= "&limit={$limit}";

return unserialize( $this->fetch($host) );

}


}

?> 

This script will extract ip information in JSON  format from geo plugin website.
now create an index.php file and put this code into index.php file.
index.php

<?php
require_once('geoPlugin.php');
$geoplugin = new geoPlugin();
$geoplugin->locate();
echo 'IP: <b>'.$geoplugin->ip.'</b><br/>';
echo 'City: <b>'.$geoplugin->city.'</b><br/>';
echo 'Region: <b>'.$geoplugin->region.'</b><br/>';
echo 'Area Code: <b>'.$geoplugin->areaCode.'</b><br/>';
echo 'dma Code: <b>'.$geoplugin->dmaCode.'</b><br/>';
echo 'Country Code: <b>'.$geoplugin->countryCode.'</b><br/>';
echo 'Country Name: <b>'.$geoplugin->countryName.'</b><br/>';
echo 'Country Name: <b>'.$geoplugin->countryName.'</b><br/>';
echo 'Continent Code: <b>'.$geoplugin->continentCode.'</b><br/>';
echo 'Latitude: <b>'.$geoplugin->latitude.'</b><br/>';
echo 'Longitude: <b>'.$geoplugin->longitude.'</b><br/>';
echo 'Currency Code: <b>'.$geoplugin->currencyCode.'</b><br/>';
echo 'Currency Symbol: <b>'.$geoplugin->currencySymbol.'</b><br/>';
echo 'Currency Converter: <b>'.$geoplugin->currencyConverter.'</b><br/>';
?>
I hope you understand this article and this article will help you. If you have any question or suggestion about Getting location using IP address in PHP article please don't hesitate to do that. Add your suggestion or question in comment section.





Search on PHPAns
Subscribe to PHPAns
Join with our 32000+ subscribers and get our latest articles in your email inbox for free.
Delivered by FeedBurner
We always respect your privacy and take protecting it seriously.
Connect Social With PHPAns
Top