1. 下载数据库和 PHP 库文件
- 下载 GeoID.dat.gz, 解压为 GeoIP.dat 文件. http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
- 下载 geoip.inc.
2.把GeoIP.dat解压放在Public里
3.把
geoip.inc
复制,放在ThinkPHP\Library\Org中命名为 GeoIP.class.php
4.修改 GeoIP.class.php
4.1 添加命名空间
<?php
namespace
Org
;
4.2修改类,将
class
GeoIP{} 包含下面的所有方法
4.3 加上public 和$this
public function
_setup_segments
(
$gi
){ }
$gi
=
$this
->
_setup_segments
(
$gi
)
;
return
$gi
;
5.调用
/**
* ip获取城市
* @return bool|string
*/
public function
getIPCountry
(
$ip
=
''
){
$ip
= empty
(
$ip
)
?
I
(
"ip"
)
:
$ip
;
// 引入 PHP 库文件
$pushObj
= new
\Org\GeoIP()
;
// 打开本地数据库, 数据保存在 GeoIP 文件中.
$tmp
=
APP_PATH
.
'../Public/GeoIP.dat'
;
$geoData
=
$pushObj
->
geoip_open
(
$tmp
,
GEOIP_STANDARD
)
;
// 获取国家名称
$countryName
=
$pushObj
->
geoip_country_name_by_addr
(
$geoData
,
$ip
)
;
// 关闭本地数据库
$pushObj
->
geoip_close
(
$geoData
)
;
$country
=
'其他区域'
;
if
(
$countryName
){
$country
=
$countryName
;
}
return
$country
;
}