ThinkPHP判断IP地区位置

2023-05-21,

1.在Action目录下添加如下代码

点击(此处)折叠或打开

  1. public function index(){
  2.         $where = 'TRUE';
  3.         $membersModel = new Model("Members");
  4.         $rows = $membersModel->where($where)->select();
  5.         
  6.         if($rows){
  7.             import('ORG.Net.IpLocation');// 导入IpLocation类
  8.             $Ip = new IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件。下载dat文件到net目录下
  9.             foreach ($rows as $k=>$v){
  10.                 $area = $Ip->getlocation($v['regIP']);
  11.                 $rows[$k]['location'] = isset($area['country'])?$area['country']:'未知地址';// 获取某个IP地址所在的位置
  12.             }
  13.         }
  14.         $this->assign('rows',$rows);
  15.         $this->display();
  16.     }

2.在html页面上添加

点击(此处)折叠或打开

  1. <table class="table table-striped table-hover table-condensed">
  2.             <tr>
  3.                 <th>用户名</th>
  4.                 <th>邮箱</th>
  5.                 <th>注册时间</th>
  6.                 <th>注册IP</th>
  7.                 <th>激活状态</th>
  8.             </tr>
  9.             
  10.             <volist name="rows" id="v">
  11.                 <tr>
  12.                     <th>{$v.username}</th>
  13.                     <th>{$v.email}</th>
  14.                     <th>{:date('Y-m-d H:i:s',$v['regTime'])}</th>
  15.                     <th>{$v.location}{$v.regIP}</th>
  16.                     <th><eq name="v.status" value="0"><span style="color: red">未激活</span><else/><span style="color:blue">已激活</span></eq></th>
  17.                 </tr>
  18.             </volist>
  19.         </table>

《ThinkPHP判断IP地区位置.doc》

下载本文的Word格式文档,以方便收藏与打印。