php 없이 자바스크립트 만으로 사용자 ip 얻기

· 2018-10-06 (토) 16:27:41 · 8530 · 5

[code]

<script>
function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
    //compatibility for firefox and chrome
    var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
    var pc = new myPeerConnection({
        iceServers: []
    }),
    noop = function() {},
    localIPs = {},
    ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
    key;

    function iterateIP(ip) {
        if (!localIPs[ip]) onNewIP(ip);
        localIPs[ip] = true;
    }

     //create a bogus data channel
    pc.createDataChannel("");

    // create offer and set local description
    pc.createOffer().then(function(sdp) {
        sdp.sdp.split('\n').forEach(function(line) {
            if (line.indexOf('candidate') < 0) return;
            line.match(ipRegex).forEach(iterateIP);
        });
        
        pc.setLocalDescription(sdp, noop, noop);
    }).catch(function(reason) {
        // An error occurred, so handle the failure to connect
    });

    //listen for candidate events
    pc.onicecandidate = function(ice) {
        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
        ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
    };
}

// Usage

getUserIP(function(ip){
    document.getElementById("ip").innerHTML = '<font color="#fff">' + 'Your IP : '  + ip + '</font>'; //이 부분을 수정하면 표시 내용을 변경할수 있습니다.
});
</script>

[/code]

ip 표시를 원하는곳에 아래를 넣으면 됩니다.

[code]

<span id="ip"></span> 

[/code]

 

|

댓글 5개

2018-10-06 (토) 19:44:41
모바일 사파리에서 작동 불능이네요..
2018-10-07 (일) 12:40:48
이거 되게 신기하네요. 로컬IP만 알 수 있는건가여?
감사합니다
2018-10-08 (월) 20:25:56
예시사이트가 안보이네요~
2018-10-14 (일) 17:04:43
감사합니다.
댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

2,788건
+
제목 글쓴이 날짜 조회
18-10-23 조회 1.3만
18-10-22 조회 9,811
18-10-21 조회 6,590
18-10-21 조회 6,224
18-10-20 조회 6,141
18-10-18 조회 7,189
18-10-17 조회 7,847
18-10-15 조회 6,399
18-10-14 조회 1.2만
18-10-12 조회 1.7만
18-10-10 조회 6,640
18-10-10 조회 7,678
18-10-10 조회 5,191
18-10-08 조회 5,625
18-10-08 조회 1.1만
18-10-06 조회 8,531
18-10-04 조회 1.4만
18-10-03 조회 7,147
18-10-02 조회 6,921
18-10-02 조회 6,160
18-09-29 조회 5,643
18-09-28 조회 7,130
18-09-21 조회 1만
18-09-20 조회 9,034
18-09-17 조회 7,310
18-09-13 조회 7,543
18-09-12 조회 6,368
18-09-05 조회 8,941
18-09-05 조회 6,721
18-09-02 조회 9,626