Hi All, I want to know how to get client IP address using jQuery?is it possible?I know pure javascript cant, but got one code using JSONP from Stack overflow itself .so, is there any workaround using jQuery? Thanks for ur help.
+6
A:
jQuery can handle JSONP, just pass an url formatted with the callback=? paramtere to the $.getJSON method, for example:
$.getJSON("http://jsonip.appspot.com?callback=?",
function(data){
alert( "Your ip: " + data.ip);
});
This example is of a really simple JSONP service implemented on Google App Engine, you can see more details here.
Check the source of the service, is a small Python script, it can be implemented on any server-side language.
If you aren't looking for a cross-domain solution the script can be simplified even more, since you don't need the callback parameter, and you return pure JSON.
Run the above snippet here.
CMS
2009-10-29 06:12:36
will give it a try.Thanks.
Wondering
2009-10-29 12:42:31
+1
A:
A simple AJAX call to your server, and then the serverside logic to get the ip address should do the trick.
$.getJSON('getip.php', function(data){
alert('Your ip is: ' + data.ip);
});
Then in php you might do:
<?php
/* getip.php */
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
print json_encode(array('ip' => $ip));
Alex Sexton
2009-10-29 06:16:23