thinkphp版根据ip获取登录地址记录方便记录后台用户登录情况

 1 <?php
 2 class LoginLogAction extends Action {
 3 
 4     // public function _initialize() {
 5     //     parent::_initialize();
 6     //     // $this->_mod = D('menu');
 7     // }
 8 
 9     public function index() {
10         $limit = $_POST['limit'];
11         if(!$limit){
12             $limit = 20;//默认查询20条
13         }
14         $log = M('admin_login_log')->order('id DESC')->limit($limit)->select();
15         $count = $limit;
16         $this->assign('count',$count);
17         $this->assign('list',$log);
18         $this->display();
19 
20         
21     }
22     public function findAddressByIp(){
23         $ip = $_POST['ip'];
24         $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
25         if(empty($res)){ return false; }  
26         $jsonMatches = array();  
27         preg_match('#\{.+?\}#', $res, $jsonMatches);  
28         if(!isset($jsonMatches[0])){ return false; }  
29         $json = json_decode($jsonMatches[0], true);  
30         if(isset($json['ret']) && $json['ret'] == 1){  
31             $json['ip'] = $ip;  
32             unset($json['ret']);  
33         }else{  
34             return false;  
35         }  
36         //return $json;  
37         //p($json);
38         echo json_encode($json['country'].$json['province'].$json['city']);
39      
40     }
41 
42     
43 }

以上是后端代码

 

 

前端代码展示

  1 <!DOCTYPE html>
  2 <html>
  3 <head>
  4     <meta charset="utf-8">
  5     <title>登录记录byXk2017.4.13</title>
  6     <link rel="stylesheet" type="text/css" href="http://apps.bdimg.com/libs/todc-bootstrap/3.1.1-3.2.1/todc-bootstrap.min.css">
  7     <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript" charset="utf-8" async defer></script>
  8     <script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js" type="text/javascript" charset="utf-8" async defer></script>
  9     <style type="text/css" media="screen">
 10         table tr td{text-align:center; }
 11     </style>
 12 </head>
 13 <body>
 14 <form action="" method="post" accept-charset="utf-8">
 15     <table>
 16         
 17             <tr>
 18                 <td>
 19                         想查看的条数<input type="text" name="limit"> <input type="submit" name="确认">[默认为20条]
 20                 </td>
 21             </tr>
 22         
 23     </table>
 24         
 25 </form>
 26 
 27 
 28 <table class="table table-striped table-hover" style="width: 500px;">
 29     
 30     <thead>
 31         <tr>
 32             <th>ID</th><th>用户名</th><th>时间</th><th>IP</th><th>接口获取地段</th>
 33         </tr>
 34     </thead>
 35     <tbody>
 36     <volist name="list" id="vo">
 37         <tr>
 38             <td>{$i}</td>
 39             <td>{$vo.username}</td>
 40             <!-- <td>{$vo.login_time|date="Y-m-d H:i:s",###}</td> -->
 41             <td>{$vo.login_time|frienddate}</td>
 42             <td class="ip{$i}">{$vo.login_ip}</td>
 43             <td class="address{$i}"></td>
 44         </tr>
 45         <tr>
 46             <td colspan="5" rowspan="" headers="">&nbsp;</td>
 47         </tr>
 48     </volist>
 49     </tbody>
 50 </table>
 51 
 52 <h5 class="h5"></h5>
 53 <button type="" class="test" onclick="test();">test</button>
 54 <script>
 55 var cou = {$count};
 56 window.onload=function(){ 
 57     for (var i = 1; i <= {$count}; i++) {
 58         getAddress(i);
 59     }
 60 } 
 61 
 62 function getAddress(i){
 63     var ip = $('.ip'+i).html();
 64      // alert(ip);
 65     var url = "{:U('LoginLog/findAddressByIp')}";
 66     $.ajax({
 67         url : url,
 68         data : {'ip':ip},
 69         dataType : 'json',
 70         type : 'post',
 71         success : function (data) {
 72             // alert(data);
 73             $('.address'+i).html(data);
 74         },
 75         error:function () {      
 76                 alert("请求失败!");
 77         }
 78     });
 79     
 80 }
 81 function test(){
 82     // alert($);
 83     var url = "{:U('LoginLog/findAddressByIp')}";
 84     $.ajax({
 85         url : url,
 86         data : {'ip':'171.94.30.254'},
 87         dataType : 'json',
 88         type : 'post',
 89         success : function (data) {
 90             alert(data);
 91         },
 92         error:function () {      
 93                 alert("请求失败!");
 94         }
 95     });
 96 }
 97 
 98     // // var url="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip="+ip;
 99     
100 
101 </script> 
102 
103 
104 
105 
106 </body>
107 </html>

 

posted @ 2017-04-13 16:03  韩宇————————  阅读(919)  评论(0编辑  收藏  举报