Home >Backend Development >PHP Tutorial >geoip+php example: get country name and code through ip
How to use GeoIP + PHP
Method 1:
Download the PHP file geoip.inc of GeoIP and save it as geoip.inc.php
http://sjolzy.cn/php/GeoIP/bak/geoip.inc
php usage code
<?php include("geoip.inc.php"); $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD); $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); $country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']); geoip_close($gi); $jsonEcho = array(); $jsonEcho["error"] = 0; $jsonEcho["country_code"] = $country_code; $jsonEcho["country_name"] = $country_name; function jsonp($object, $callback = 'callback') { if (!empty($_GET[$callback])) { header('Content-Type: application/x-javascript'); } else { header('Content-Type: application/json'); } return $_GET[$callback].'('.json_encode($object).')'; } echo jsonp($jsonEcho); ?>
Reference: http://blog.csdn.net/prince2270/article/details/4592753
The above introduces the geoip+php example: obtaining the country name and code through IP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.