Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get client IP address via third party web service

Tags:

javascript

ip

I would like to read my ip address from the following page(http://l2.io/ip or other) using javascript to save him in my variable "myIp".

function getMyIP() {
  var myIp;
  ...
  return myIp;
}

How can you do?

like image 482
Milton90 Avatar asked Jul 01 '13 22:07

Milton90


People also ask

How can we get the IP address of the client user?

getenv() function: The other way to get the IP address of the client is using the getenv() function. It is used to retrieve the value of an environment variable in PHP. To get the IP address of the user we need to pass the REMOTE_ADDR variable to the getenv() function. getenv("REMOTE_ADDR");

Can you get IP address from http request?

You can use RemoteAddr to get the remote client's IP address and port (the format is "IP:port"), which is the address of the original requestor or the last proxy (for example a load balancer which lives in front of your server). This is all you have for sure. This is because internally http. Header.

How to get the client’s public IP address?

The networking part of the Internet is defined by exact specifications (guidelines) for connecting on the Internet. To get the client’s public IP address, JavaScript acts as a third-party server-side language. JavaScript can’t do it alone, so, jQuery is added to do this.

How to get the client IP address using only JavaScript?

Learn how to get the client IP address (local and private) using only javascript. Javascript is unable to get (nor stores somewhere) the client IP, however javascript is able to create Http requests, and server side languages are able to retrieve the user public IP, so you could use this as advantage.

How can I get the IP address of a user?

Let’s begin with the simplest way: applying the $_SERVER ['REMOTE_ADDR']. It returns the user IP addresses. It can be run in the following way:

How to retrieve the private IP of the user using WebRTC?

However, with the introduction of the WebRTC, you'll be able to retrieve the private IP of the user with a trick using RTCPeerConnection. In this article you'll learn how to retrieve the user IP (private using pure javascript and public using a third party service) easily with a couple of tricks.


3 Answers

Checking your linked site, you may include a script tag passing a ?var=desiredVarName parameter which will be set as a global variable containing the IP address:

<script type="text/javascript" src="http://l2.io/ip.js?var=myip"></script>
                                                      <!-- ^^^^ -->
<script>alert(myip);</script>

Demo

I believe I don't have to say that this can be easily spoofed (through either use of proxies or spoofed request headers), but it is worth noting in any case.


HTTPS support

In case your page is served using the https protocol, most browsers will block content in the same page served using the http protocol (that includes scripts and images), so the options are rather limited. If you have < 5k hits/day, the Smart IP API can be used. For instance:

<script>
var myip;
function ip_callback(o) {
    myip = o.host;
}
</script>
<script src="https://smart-ip.net/geoip-json?callback=ip_callback"></script>
<script>alert(myip);</script>

Demo

Edit: Apparently, this https service's certificate has expired so the user would have to add an exception manually. Open its API directly to check the certificate state: https://smart-ip.net/geoip-json


With back-end logic

The most resilient and simple way, in case you have back-end server logic, would be to simply output the requester's IP inside a <script> tag, this way you don't need to rely on external resources. For example:

PHP:

<script>var myip = '<?php echo $_SERVER['REMOTE_ADDR']; ?>';</script>

There's also a more sturdy PHP solution (accounting for headers that are sometimes set by proxies) in this related answer.

C#:

<script>var myip = '<%= Request.UserHostAddress %>';</script>
like image 135
Fabrício Matté Avatar answered Sep 25 '22 06:09

Fabrício Matté


    $.ajax({
        url: '//freegeoip.net/json/',
        type: 'POST',
        dataType: 'jsonp',
        success: function(location) {
            alert(location.ip);
        }
    });

This will work https too

like image 31
Ranjit Kumar Avatar answered Sep 24 '22 06:09

Ranjit Kumar


A more reliable REST endpoint would be http://freegeoip.net/json/

Returns the ip address along with the geo-location too. Also has cross-domain requests enabled (Access-Control-Allow-Origin: *) so you don't have to code around JSONP.

like image 40
Dayson Avatar answered Sep 26 '22 06:09

Dayson