2

I'm writing a browser extension using Kango framework.

As part of its functionality it must know where the user is located. I have that part covered with an API, but the said API requires an ip address as one of the params.

Is there a way to sort of internally see what it is (or what it was last time) or do I have to make a separate call to ANOTHER service (a simple php page I suppose) to figure out what the ip is?

The extension will make another call after the location is established. Should I be irked about 3 separate calls?

Thanks.

Jake
  • 43
  • 5

2 Answers2

3

Javascript:

$.ajax({
        type: "POST",
        url: "getIP.php",
        data: "",
        dataType: 'json',
        success: function(ret) {
           alert("ip = " + ret);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            console.error(xhr.responseText);
            console.error(thrownError);
        } 
    });

PHP:

if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
    $IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
    $IP = $_SERVER['REMOTE_ADDR'];
echo json_encode($IP);

Sidenote: I am using jQuery here, read the docs here if you don't know how to use it: http://docs.jquery.com/Main_Page

Hidde
  • 11,493
  • 8
  • 43
  • 68
1

Yes, for security reasons you can't know the IP of the user via Javascript, so you have to rely on a server-side service.

MaxArt
  • 22,200
  • 10
  • 82
  • 81