Technology Programming

How To Get Visitor's Ip And Country Code With PHP

Original source : How To Get Visitor's Ip And Country Code With PHP

Have you wanted a complete data such as IP and your visitors Country from visitors? Many applications of statistics site that offers this feature as http://histats.com or http://statcounter.com. But what if you want the ability and Country IP detection is integrated with your PHP applications?

Here we will learn how to get data from the visitor's IP and Country by Country Geolite assistance which is available free from http://maxmind.com. Maxmind itself offers a variety of programming languages that can be integrated through their APIs, including C, C# Class, Java Class, MS the COM Object, Pascal, PERL, Python, Ruby, VB.Net, and PHP. Or you can also integrate their API through Javascript.

First please download GeoLite Country of http://www.maxmind.com/app/geolitecountry that contains the database from the list of country around the world. This database is regularly updated by Maxmind first date at the beginning of each month. Choose the type of binary format or by copying the following URL directly into your download manager, http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz.

Second, we downloaded the PHP API them from the following URL, http://geolite.maxmind.com/download/geoip/api/php/ or by copying the following URL directly into your download manager, http://geolite.maxmind.com/download/geoip/fire/php/geoip.inc. Now we have to have every thing that is required, it is time to integrate with our PHP applications.

Please extract GeoIP.dat.gz t into a directory in your Web server, so we'll get a file named Geo.IP.dat. Copy also geoip.inc file into the same directory with the files. Dat earlier. So now both files are in a same directory. Then create an index.php file in the same directory to try running GeoIP functions.

[php]

Original source : How To Get Visitor's Ip And Country Code With PHP

// require geoip.inc api
require_once('geoip.inc');

// open GeoIP.dat
$open = geoip_open("GeoIP.dat", GEOIP_STANDARD);

// get visitor's IP and Country code (two letters code)
echo "IP address 75.126.221.216 located in " . geoip_country_name_by_addr($open, "75.126.221.216") . " (country code " . geoip_country_code_by_addr($open, "75.126.221.216") . ")";

// close database handler
geoip_close($open);

// print license notice
echo "

-- This product includes GeoIP data created by MaxMind, available from http://maxmind.com/ --";

?>
[/php]

Index.php That's so simple to get the visitor's IP and Country code. Now how to make it dynamic by always checking the IP of each visitor? We will create a function to get the IP.

[php]
function get_ip()
{
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]

Now we join the two code above to capture the visitor's IP Address and Country in a dynamic and realtime.

[php]
function get_ip()
{
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;
}

// require geoip.inc api
require_once('geoip.inc');

// open GeoIP.dat
$open = geoip_open("GeoIP.dat", GEOIP_STANDARD);

// get visitor's IP and Country code (two letters code)
echo "IP address ".get_IP()." located in " . geoip_country_name_by_addr($open, get_IP()) . " (country code " . geoip_country_code_by_addr($open, get_IP()) . ")";

// close database handler
geoip_close($open);

// print license notice
echo "

-- This product includes GeoIP data created by MaxMind, available from http://maxmind.com/ --";
?>

[/php]

Bingo, everything has been completed and you can memodifikasikan fit your needs. Happy coding:)

How To Get Visitor's Ip And Country With PHP

How To Get Visitor's Ip And Country With PHP

Original source : How To Get Visitor's Ip And Country Code With PHP

Related posts "Technology : Programming"

Brochure Design And Its Budget

Programming

Website designing delhi-web design services India-Website Development Company India

Programming

The Secrets and techniques Rob Fore Won't Tell you!

Programming

Where Do You Get Podcasting Ideas?

Programming

Avoid Hacking With The Help of WordPress Development Company

Programming

Sirius Radio - All You At Any Time Wished to Know

Programming

How to Use a Check Box to Filter a List

Programming

Get professional help from website development Dublin- promote business growth

Programming

PHP Shopping Carts

Programming

Leave a Comment