(1 Introduction
When I use the IpLocation method getLocation of the thinkphp class library, I found that the obtained information is empty.
After analyzing the query, we learned that the reason is that the address library UTFWry.dat is not loaded in the framework. You can see __construct($filename = "UTFWry.dat") in the construction method, you need to download the file, and decompress it to get UTFWry.dat, put the file in
$this->fp = fopen(dirname(__FILE__).'/'.$filename, 'rb'))
Corresponding address
ThinkPHP \ Library \ Org \ Net / UTFWry.dat 中
Then the getlocation(ip address) method can get the address
(2) Subject
There is a get_client_ip() under ThinkPHP\Common\funcitons.php to get the ip address
But sometimes it's not accurate enough
Found the following paragraph
function get_client_ip($type = 0) { $type = $type ? 1 : 0; static $ip = NULL; if ($ip !== NULL) return $ip[$type]; if($_SERVER['HTTP_X_REAL_IP']){//In nginx proxy mode, get the real IP of the client $ip=$_SERVER['HTTP_X_REAL_IP']; }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//Client's ip $ip = $_SERVER['HTTP_CLIENT_IP']; }elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//The gateway of the user's computer browsing the current page $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown',$arr); if(false !== $pos) unset($arr[$pos]); $ip = trim($arr[0]); }elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR'];//The ip address of the user's computer browsing the current page }else{ $ip=$_SERVER['REMOTE_ADDR']; } // IP address legal verification $long = sprintf("%u",ip2long($ip)); $ip = $long ? array($ip, $long) : array('0.0.0.0', 0); return $ip[$type]; }
Now we can use
$ip=get_client_ip();
Now we want to get the user's location based on the ip address
Thinkphp supports the IP location function, it needs to use the extended class library Org\Net\IpLocation, and it should be used together with the IP address library file
$Ip = new \Org\Net\IpLocation('UTFWry.dat'); // The instantiated class parameter represents the IP address library file $area = $Ip->getlocation(); // Get the location of an IP address
This UTFWry.dat needs us to download
http://pan.baidu.com/s/1eSz1GTO Password: 7q4v, decompress and put it in the ThinkPHP\Library\Org\Net directory
Now I output the $area information
array(5) { ["ip"] => string(13) "218.79.93.194" ["beginip"] => string(11) "218.79.93.0" ["endip"] => string(13) "218.79.94.255" ["country"] => string(18) "Putuo District, Shanghai" ["area"] => string(20) "/Jing'an District Telecom ADSL" }
.