Get client IP address via third party web service

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

function getMyIP() {
  var myIp;
  return myIp;

How can you do?



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=""></script>
                                                      <!-- ^^^^ -->


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:

var myip;
function ip_callback(o) {
    myip =;
<script src=""></script>


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:

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:


<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.


<script>var myip = '<%= Request.UserHostAddress %>';</script>
        url: '//',
        type: 'POST',
        dataType: 'jsonp',
        success: function(location) {

This will work https too


A more reliable REST endpoint would be

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.


If you face an issue of CORS, you can use

function httpGet(theUrl)
    var xmlHttp = new XMLHttpRequest(); "GET", theUrl, false );
    xmlHttp.send( null );
    return xmlHttp.responseText;

publicIp = httpGet("");
alert("Public IP: " + publicIp);

I agree that using synchronous HTTP call is not good idea. You can use async ajax call then.


This pulls back client info as well.

var get = function(u){
    var x = new XMLHttpRequest;'GET', u, false);
    return x.responseText;


Well, if in the HTML you import a script...

<script type="text/javascript" src="//"></script>

You can then use the variable userIP (which would be the visitor's IP address) anywhere on the page.

To redirect: <script>if (userIP == "555.555.555.55") {window.location.replace("");}</script>

Or to show it on the page: document.write (userIP);

DISCLAIMER: I am the author of the script I said to import. The script comes up with the IP by using PHP. The source code of the script is below.

<?php //Gets the IP address $ip = getenv("REMOTE_ADDR") ; Echo "var userIP = '" . $ip . "';"; ?>

    <script type="application/javascript">
            function getip(json){
            alert(json.ip); // alerts the ip address

    <script type="application/javascript" src=""></script>


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.