Obtain client IP from javascript or jquery

7

How can I get the% co_of real% of the client for% co_of% if possible.

This means that there is a direct connection, or by means of a IP , or Javascript or whatever, I always identify the proxy real of the client computer.

Implement this code in javascript. But it only works in some cases, and it is that it has no proxy, so in the personal it does not work for me, because it points to the proxy and not to the router of my machine.

Code:

<!DOCTYPE html>
<html>
  <head>
    <title>IP real</title>
  </head>
  <body>
     Mi IP es: <strong id="ipId"></strong>

    <script type="text/javascript">
        function get_ip(obj){
            document.getElementById('ipId').innerHTML = obj.ip;
        }
    </script>
    <script type="text/javascript" src="https://api.ipify.org/?format=jsonp&callback=get_ip"></script>
  </body>
</html>
    
asked by José Miguel Sepulveda 03.05.2017 в 22:44
source

4 answers

2

Using JS , one option would be to use the API of a Web Service, for example Freegeoip , which accepts three response formats JSON XML CSV , the URL is specified as follows.

freegeoip.net/{format}/{IP_or_hostname}

$.getJSON('//freegeoip.net/json/?callback=?', function(data) {
  console.log(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  

There are more alternatives, some are specified in    this Answer

    
answered by 03.05.2017 в 23:12
1

Look at this question link . One of the responses recommends using WebRTC. Chrome on iOS, Internet Explorer and Safari have not implemented WebRTC yet.

WebRTC has a bug that makes it possible for you to obtain a user's IP even if you are using a VPN (virtual private network). In other words, this failure can be corrected someday and once corrected this method will no longer work. It is also possible to disable the WebRTC in Firefox, so you will not be able to obtain the IP of those users. Here you can try your browser link

The simplified code in that answer that only gives you an IP is:

var findIP = new Promise(r=>{var w=window,a=new (w.RTCPeerConnection||w.mozRTCPeerConnection||w.webkitRTCPeerConnection)({iceServers:[]}),b=()=>{};a.createDataChannel("");a.createOffer(c=>a.setLocalDescription(c,b,b),b);a.onicecandidate=c=>{try{c.candidate.candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g).forEach(r)}catch(e){}}})

/*Ejemplo de uso*/
findIP.then(ip => document.write('Tu IP: ', ip)).catch(e => console.error(e))

The code that can give you several IP of the user depending on your network is:

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;

  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;
  ul.appendChild(li);
}

findIP(addIP);

The original code is from link

    
answered by 05.05.2017 в 22:21
0

you can do it with variable $_SERVER[] , in this case $_SERVER["REMOTE_ADDR"] , example

<html>
 <head>
 <title>Current IP Check</title>
 </head>
 <body>
  Current IP Address: <?php  echo $_SERVER['REMOTE_ADDR']; ?>
 </body>
</html>
    
answered by 03.05.2017 в 23:11
0

This is the best answer I've found in stackoverflow (English) in both Javascript and jQuery in https secure environment using an external free API:

Obtain client IP in JAVASCRIPT:

<script type="application/javascript">
  function getIP(json) {
    document.write("My public IP address is: ", json.ip);
  }
</script>

<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>

Obtain client's IP in JQUERY:

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

I have not tried Javascript but in Jquery it's perfect. Test if you get your IP right by just writing this in your browser: link

SOURCE: link

    
answered by 23.11.2018 в 11:52