TimOkay
TimOkay

Reputation:

JavaScript: How to get users IP ADDRESS?

Is it possible using only JavaScript to obtain the user's IP Address? If so, how?

Upvotes: 4

Views: 8321

Answers (4)

Jacob
Jacob

Reputation: 59

I've actually been toying around with this myself. You can obtain the user's local IP address if they are using certain browsers using JavaScript with WebRTC. WebRTC is currently supported by Chrome, Firefox, and Opera, so it doesn't work across all browsers, but its a start. A great solution is offered by mido in an earlier question titled How to get client’s IP address using javascript only?.

This is the code I am currently attempting to alter, so I can save the IP addresses to variables instead of just displaying them. I haven't figured that part out yet, but it should help you out. Just cut and paste to a text file and open in your browser.

<html>
<body>

 <p id=saveIP> Replace this with IP </p>

<script>

function findIP(onNewIP) { //  onNewIp - your listener function for new IPs
  var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
  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;
    //window.saveIP = pc;
    //window.saveIP = localIPs; // Returns [object, object] or JSON.stringfy returns {}

  function ipIterate(ip) {
    if (!localIPs[ip]) onNewIP(ip);
    localIPs[ip] = true;  
  }
  pc.createDataChannel(""); //create a bogus data channel
  pc.createOffer(function(sdp) {
    sdp.sdp.split('\n').forEach(function(line) {
      if (line.indexOf('candidate') < 0) return;
      line.match(ipRegex).forEach(ipIterate);
});
    pc.setLocalDescription(sdp, noop, noop);
  }, noop); // create offer and set local description
  pc.onicecandidate = function(ice) { //listen for candidate events
    if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
    ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
  };
}


var ul = document.createElement('ul');
ul.textContent = 'Your IPs are: '
document.body.appendChild(ul);

function addIP(ip) {
  console.log('got ip: ', ip);
  var li = document.createElement('li');
  li.textContent = ip;
  window.saveIP = ip;  // <--value captured is [object HTMLParagraph]; JSON.stringify returns {}
  ul.appendChild(li);
}

findIP(addIP);
document.getElementById('saveIP').innerHTML = JSON.stringify(window.saveIP);
</script>
</body>
</html>

Upvotes: 1

Irwin
Irwin

Reputation: 12819

What about using one of those online services that tell you the IP address of the requestor. I've only seen them in the context of a html view. But my idea is that you make some request, and the response would contain the ip info (in some crude way).

Upvotes: 0

Paulo
Paulo

Reputation: 4333

No. It is strictly client-side so it will use some secondary technology to find the IP Address.

A google search provides many options.

Upvotes: 0

alex
alex

Reputation: 490123

I don't think so. You'll need to use a server side language. Or find a service maybe you could use with AJAX, but I'm not sure if one exists.

Upvotes: 1

Related Questions