0

I am using javascript to determine the visitors ip address. For what ever reason, it works in Chrome and not in Firefox, IE, or other browsers.

Here is my code:

function getIPAddress() {
    var xmlHttp;

    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    } else {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlHttp.open("GET", "http://api.hostip.info/get_html.php", false);
    xmlHttp.send();

    var hostipInfo = xmlHttp.responseText.split("\n");

    for (var i = 0; i < hostipInfo.length - 1; i++) {
        var ipAddress = hostipInfo[i].split(":");
        if (ipAddress[0] == "IP") return ipAddress[1];
    }

    return "unknown";
}

At the company I'm working for, I am behind a proxy. Could this be a proxy issue, or is there something wrong with this code? Thanks.

Just deployed my code to our test environment, and in IE, I receive a pop up saying 'This page is accessing information that is not under its control. This poses a security risk. Do you want to continue?' If I say, yes, it works. If I say, no, it doesn't.

2
  • Did you try a POST method? Maybe it is a caching issue.
    – Prusse
    Jul 5, 2011 at 13:37
  • 1
    Two things: if that's running in a page from a domain other than "hostip.info", it won't work due to basic browser security rules, and (2) you're not waiting for the ajax request to finish anyway.
    – Pointy
    Jul 5, 2011 at 13:40

3 Answers 3

0

If you're going to use AJAX (which is what this code is), I STRONGLY suggest you use a 3rd party wrapper like jQuery. That will increase cross-browser compatibility greatly and allows you to shrink your code down to something like this.

$.post('http://api.hostip.info/get_html.php', function(data){
    alert(data);
});

Additional Point

As Pointy mentioned, if your page is running on a different domain than hostip.info, you will need to setup a local PHP to fetch the data like this..

localGetData.php

die(file_get_contents('http://api.hostip.info/get_html.php'));

New Ajax

$.post('localGetData.php', function(data){
    alert(data);
});
3
  • could you give an example of setting up a local fetch the data from asp.net. I'm not using PHP and don't know the equivalent.
    – user619891
    Jul 5, 2011 at 13:58
  • Im not particularly familiar with ASP.NET, so I really could not off the top of my head. Maybe this link will suffice? wiki.asp.net/page.aspx/285/httpwebrequest/rev/4
    – Dutchie432
    Jul 5, 2011 at 14:02
  • 1
    just realized i could get ip from server side with Request.UserHostAddress. at moment, get address of local machine, but i think that's because my computer and server both inside the same network.
    – user619891
    Jul 5, 2011 at 14:23
0

for (var i = 0; i < hostipInfo.length - 1; i++) { will cause to return "unknow" if hostipInfo has only one element, change to for (var i = 0; i < hostipInfo.length; i++) {.

0
0

The reason this works in Chrome is because it supports CORS which allows you to make this request cross domain.

Since the site doesnt appear to support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.