瀏覽代碼

用户登录,保存ip获取到的地址

lizhen_gitee 1 年之前
父節點
當前提交
c0c114e46c
共有 3 個文件被更改,包括 31 次插入0 次删除
  1. 24 0
      application/common.php
  2. 2 0
      application/common/library/Auth.php
  3. 5 0
      application/index/controller/Test.php

+ 24 - 0
application/common.php

@@ -750,3 +750,27 @@ if (!function_exists('last_week')) {
         return $arr;
     }
 }
+
+//ip138接口,ip地址获取实际地址
+function ip_to_address(){
+    $ip = request()->ip();
+    //$ip = '112.6.63.60';
+    $ipaddress = '火星';
+    // http协议:http://api.ip138.com/ip/
+    // https协议:https://api.ip138.com/ip/
+
+    $url = 'http://api.ip138.com/ip/?ip='.$ip.'&datatype=jsonp&token=010e8e53e47166c0623380697e7540e4';
+    $result = json_decode(curl_get($url),true);
+
+    if(is_array($result) && !empty($result)){
+        if(isset($result['ret']) && $result['ret'] == 'ok'){
+            if(isset($result['data']) && is_array($result['data']) && !empty($result['data'])){
+
+                if(isset($result['data'][2]) && !empty($result['data'][2])){
+                    $ipaddress = $result['data'][2];
+                };
+            }
+        }
+    }
+    return $ipaddress;
+}

+ 2 - 0
application/common/library/Auth.php

@@ -350,6 +350,8 @@ class Auth
                     Token::clear($user->id);
                 }
 
+                $user->ipaddress = ip_to_address();
+
                 $ip = request()->ip();
                 $time = time();
                 //记录本次登录的IP和时间

+ 5 - 0
application/index/controller/Test.php

@@ -14,4 +14,9 @@ class Test extends Controller
         $Gateway::sendToAll('来自服务端的主动推送'.date('Y-m-d H:i:s'));
 
     }
+
+    public function ip(){
+        $ipaddress = ip_to_address();
+        echo $ipaddress;
+    }
 }