Несколько фрагментов кода для получения клиентского IP с использованием JavaScript / jQuery.
1. В одну сторону
Этот дает вам долготу / широту и часовой пояс.
$(document).ready( function() { $.getJSON( "http://smart-ip.net/geoip-json?callback=?", function(data){ alert( data.host); } );});
Возвращает:
?( { source: "smart-ip.net", host: "14.200.158.65", lang: "en", countryName: "Australia", countryCode: "AU", city: "South Sydney Municipality", region: "New South Wales", latitude: "-33.9000", longitude: "151.2000", timezone: "Australia/NSW" } )
2. Два пути
Этот обеспечивает JSON.
function myIP() { 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(); hostipInfo = xmlhttp.responseText.split("n"); for (i=0; hostipInfo.length >= i; i++) { ipAddress = hostipInfo[i].split(":"); if ( ipAddress[0] == "IP" ) return ipAddress[1]; } return false; } // console.log(myIP());
Возвращает:
{ country_name: "UNITED STATES", country_code: "US", city: "(Unknown city)", ip: "14.200.158.65" }
3. Три пути
http://l2.io/ip.js Syntax : http://l2.io/ip Diplay Client IP address --> xxxx Syntax : Display Client IP address in your HTML page using javascript --> document.write('xxxx'); Syntax : Set javascript variable "myip" to client IP address --> myip = "xxxx";
http://l2.io/ip.js Syntax : http://l2.io/ip Diplay Client IP address --> xxxx Syntax : Display Client IP address in your HTML page using javascript --> document.write('xxxx'); Syntax : Set javascript variable "myip" to client IP address --> myip = "xxxx";
http://l2.io/ip.js Syntax : http://l2.io/ip Diplay Client IP address --> xxxx Syntax : Display Client IP address in your HTML page using javascript --> document.write('xxxx'); Syntax : Set javascript variable "myip" to client IP address --> myip = "xxxx";