Page 1 of 1

IP

Posted: 26 Oct 2015
by Sebastian Haugland
Hi

I would like to see the IP of the client (to determine the location of the user), any ideas?

Re: IP

Posted: 27 Oct 2015
by rostislav
Here are a couple of options:

1. Use a REST service, e.g.

Code: Select all

$.ajax({
       url: "http://freegeoip.net/json/"
    }).then(function(data) {
       console.log(data.ip); //data contains other things, like country, latitude, longitute, etc
    });
2. Append a 3rd party script to your page: Add this code on form load:

Code: Select all

$('head').append('<script type="text/javascript" src="http://l2.io/ip.js?var=myip"></script>')
This will append a script to your page. Then you will be able to access the IP via myip variable, e.g.:

Code: Select all

alert(myip);
This (probably) wouldn't work if your site uses the https protocol. Also, from stackoverflow: "...this can be easily spoofed (through either use of proxies or spoofed request headers...".