API接口 为了避免重复查找,这里记录一下接口
ip.sb 获取自己IP:https://api.ip.sb/ip ip.sb IP归属地查询: https://api.ip.sb/geoip ip-api.com 接口:http://ip-api.com/json/ 域名信息查询接口:https://dnslytics.com/api 搜狐IP接口(默认 GBK):http://pv.sohu.com/cityjson 搜狐接口(UTF-8): http://pv.sohu.com/cityjson?ie=utf-8 使用 ip.sb 以及 ip-api.com 都是返回 JSON 数据 IP信息:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <script type ="application/javascript" > function getip (json ){ document .write ("My IP address is: " , json.ip ); } </script > <script type ="application/javascript" src ="https://api.ip.sb/jsonip?callback=getip" > </script > <script type ="application/javascript" > $(document ).ready (function ( ) { $.getJSON ("https://api.ip.sb/jsonip?callback=?" , function (json ) { document .write ("My IP address is: " , json.ip ); } ); }); </script >
GeoIP 信息:1 2 3 4 5 6 7 8 9 10 11 { "ip" : "185.255.55.55" , "country_code" : "NL" , "country" : "Netherlands" , "continent_code" : "EU" , "latitude" : 52.3824 , "longitude" : 4.8995 , "asn" : "3214" , "organization" : "xTom Limited" , "timezone" : "Europe/Amsterdam" , }
搜狐的那个相当于在 js 中声明变量,也就是说不能直接在其他语言里面用,而且查询到的信息有限。 1 2 3 4 5 6 <script type ="text/javascript" src ="http://pv.sohu.com/cityjson?ie=utf-8" > </script > <script type ="text/javascript" > let ip = returnCitySN.cip ; console .log (returnCitySN.cip ); </script >
实例 本站的跳转实现
思路:获取用户IP的国家代码 判断是不是中国 是,尝试访问 Cookie,若有执行对应操作,无则询问,并将结果保存并执行对应操作。不是则结束。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 <script> function redirct (geoip ) { let REDIRCT_KEY = "redirct" ; let redirct_sites = "iitii.malateam.cn" ; let confirm_string = "是否转到国内CDN加速站?(不保证永久可访问。选择\"是\"以后,如果需重新选择请清除浏览器 Cookies)" ; let country_code = "CN" ; if (country_code.split (" " ).indexOf (geoip.country_code ) >= 0 ) { let result = getCookieValueByKey (REDIRCT_KEY ); if (result === "true" ) { window .location .href = window .location .href .replace ( window .location .host , redirct_sites ); } else if (result === "false" ) { return ; } else { let question = confirm (confirm_string); setCookie (REDIRCT_KEY , question, 7 ); if (question) { window .location .href = window .location .href .replace ( window .location .host , redirct_sites ); } } } } function getCookieValueByKey (key ) { let tmp = key + "=" ; let cookies = document .cookie .split (';' ); for (let i = 0 ; i < cookies.length ; i++) { let cookie = cookies[i].trim (); if (cookie.indexOf (tmp) == 0 ) { return cookie.substring (tmp.length ); } } return "" ; } function setCookie (key, value, expireDays ) { if (expireDays > 0 || expireDays < 0 ) { let date = new Date (); date.setTime ( date.getTime () + ( expireDays * 24 * 60 * 60 * 1000 ) ); document .cookie = `${key} =${value} ; expires=${date.toGMTString()} ` ; } else { document .cookie = `${key} =${value} ` ; } } </script> <script type ="application/javascript" src ="https://api.ip.sb/geoip?callback=redirct" > </script >
-------------本文结束 再接再厉-------------
本文标题: 前端 js 获取IP归属地信息
文章作者: IITII
发布时间: 2020年06月20日 - 21:06
最后更新: 2020年08月21日 - 02:08
原始链接: https://iitii.github.io/2020/06/20/1/
许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。
打赏 微信支付
支付宝