• JavaScript获取客户端IP地址


    1. 第三方接口

    1) 这里提供一个搜狐接口的地址:http://pv.sohu.com/cityjson?ie=utf-8 ,将这个js引入到页面即可得到returnCitySN。

    2) api.ipify.org

    https://api.ipify.org/?format=jsonp&callback=getIP

    1 <script type="application/javascript">
    2   function getIP(json) {
    3     document.write("My public IP address is: ", json.ip);
    4   }
    5 </script>
    6 
    7 <script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>

    或者

    1 $.getJSON('https://api.ipify.org?format=json', function(data){
    2     console.log(data.ip);
    3 });

    2. 向服务器发送一个ajax请求,该请求包中会包含客户端的相关信息,当然也包括IP。

    3. 使用webRTC,获取私有IP

    The RTCPeerConnection() constructor returns a newly-created RTCPeerConnection, which represents a connection between the local device and a remote peer.(http://ourcodeworld.com/articles/read/257/how-to-get-the-client-ip-address-with-javascript-only)

     1 /**
     2  * Get the user IP throught the webkitRTCPeerConnection
     3  * @param onNewIP {Function} listener function to expose the IP locally
     4  * @return undefined
     5  */
     6 function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
     7     //compatibility for firefox and chrome
     8     var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
     9     var pc = new myPeerConnection({
    10         iceServers: []
    11     }),
    12     noop = function() {},
    13     localIPs = {},
    14     ipRegex = /([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
    15     key;
    16 
    17     function iterateIP(ip) {
    18         if (!localIPs[ip]) onNewIP(ip);
    19         localIPs[ip] = true;
    20     }
    21 
    22      //create a bogus data channel
    23     pc.createDataChannel("");
    24 
    25     // create offer and set local description
    26     pc.createOffer().then(function(sdp) {
    27         sdp.sdp.split('
    ').forEach(function(line) {
    28             if (line.indexOf('candidate') < 0) return;
    29             line.match(ipRegex).forEach(iterateIP);
    30         });
    31         
    32         pc.setLocalDescription(sdp, noop, noop);
    33     }).catch(function(reason) {
    34         // An error occurred, so handle the failure to connect
    35     });
    36 
    37     //listen for candidate events
    38     pc.onicecandidate = function(ice) {
    39         if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
    40         ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
    41     };
    42 }
    43 
    44 // Usage
    45 
    46 getUserIP(function(ip){
    47     alert("Got IP! :" + ip);
    48 });
  • 相关阅读:
    echarts 报表使用
    Eclipse Java注释模板设置详解
    简单实现支付密码输入框 By HL
    一个label 混搭不同颜色,不同字体的文字.. by 徐
    有关MVC设计模式 #DF
    自定义粘贴板-陈鹏
    TableView 常用技巧与功能详解
    ios 类别和扩展-赵小波
    推荐一本书--《浪潮之巅》(完整版的哦)----董鑫
    iOS block的用法 by -- 周傅琦君
  • 原文地址:https://www.cnblogs.com/fangsmile/p/7307463.html
Copyright © 2020-2023  润新知