thinkphp实现定位功能

来源:互联网 发布:同花顺软件k线图 编辑:程序博客网 时间:2024/05/29 10:50


1.http://www.thinkphp.cn/extend/223.html下载IP定位扩展类库

2.解压后存放在ThinkPHP/Library/Library/ORG/Net/该目录下

3.http://www.thinkphp.cn/extend/270.html  下载ip地址库文件,保存在的目录和ip定位扩展类库一致,默认编码为utf8

4.实例代码

class IndexController extends Controller {
    public function index(){
        $ip = get_client_ip();// 使用内置函数获取当前的ip地址
        import('ORG.Net.IpLocation');// 导入IpLocation类
        $mp = new \Org\Net\IpLocation(); // 实例化类库文件
        $location = $mp->getlocation($ip); // 传入ip地址
        print_r($location);
    }
}

5.注意事项

如果你的IP地址库是GBK编码的话,需要对返回结果进行编码转换。

$info = iconv('gbk','utf-8',$location['country'].$location['area']);

如果你使用的IP地址库文件不是UTFWry.dat(注意在Linux下面文件名的大小写也需要一致)的话,我们需要在实例化IpLocation类的时候传入地址库文件名

$Ip = new IpLocation('MyIpWry.dat'); // 传入IP地址库文件名

原博客链接:thinkphp定位功能

0 0