如何使用JavaScript获取客户端的IP地址? [英] How to get client's IP address using JavaScript?
问题描述
我需要以某种方式使用JavaScript检索客户端的IP地址;没有服务器端代码,甚至没有SSI。
I need to somehow retrieve the client's IP address using JavaScript; no server side code, not even SSI.
但是,我并不反对使用免费的第三方脚本/服务。
However, I'm not against using a free 3rd party script/service.
推荐答案
我会使用一个可以返回JSON的Web服务(与jQuery一起使事情更简单)。以下是我可以找到的所有免费活动 IP查找服务以及它们返回的信息。如果你知道了,那么请添加评论,我会更新这个答案。
I would use a web service that can return JSON (along with jQuery to make things simpler). Below are all the free active IP lookup services I could find and the information they return. If you know of any more, then please add a comment and I'll update this answer.
试一试: < a href =http://api.db-ip.com/addrinfo?api_key=bc2ab711d740d7cfa6fcb0ca8822cb327e38844f&addr=116.12.250.1 =noreferrer> http://api.db-ip.com/addrinfo?api_key= < 您的api密钥>& addr =< IP地址>
Try it: http://api.db-ip.com/addrinfo?api_key=<your api key>&addr=<ip address>
退货:
{
"address": "116.12.250.1",
"country": "SG",
"stateprov": "Central Singapore",
"city": "Singapore"
}
限制:
- 每天2,500个请求
- 不支持JSONP回调
- 需要IP地址参数
- 需要电子邮件地址才能获取API密钥
- 没有SSL(https)和免费计划
- 2,500 requests per day
- Doesn't support JSONP callbacks
- Requires IP address parameter
- Requires an email address to get your API key
- No SSL (https) with the free plan
试一试: http://gd.geobytes.com/GetCityDetails
$.getJSON('http://gd.geobytes.com/GetCityDetails?callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"geobytesforwarderfor": "",
"geobytesremoteip": "116.12.250.1",
"geobytesipaddress": "116.12.250.1",
"geobytescertainty": "99",
"geobytesinternet": "SA",
"geobytescountry": "Saudi Arabia",
"geobytesregionlocationcode": "SASH",
"geobytesregion": "Ash Sharqiyah",
"geobytescode": "SH",
"geobyteslocationcode": "SASHJUBA",
"geobytescity": "Jubail",
"geobytescityid": "13793",
"geobytesfqcn": "Jubail, SH, Saudi Arabia",
"geobyteslatitude": "27.004999",
"geobyteslongitude": "49.660999",
"geobytescapital": "Riyadh ",
"geobytestimezone": "+03:00",
"geobytesnationalitysingular": "Saudi Arabian ",
"geobytespopulation": "22757092",
"geobytesnationalityplural": "Saudis",
"geobytesmapreference": "Middle East ",
"geobytescurrency": "Saudi Riyal",
"geobytescurrencycode": "SAR",
"geobytestitle": "Saudi Arabia"
}
限制:
- 每小时16,384次请求
- 无SSL(https)免费计划
- 可以返回错误的位置(我在新加坡,而不是沙特阿拉伯)
试一试: https://json.geoiplookup.io/api
$.getJSON('https://json.geoiplookup.io/api?callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip": "116.12.250.1",
"isp": "SGPOST",
"org": "Singapore Post Ltd",
"hostname": "116.12.250.1",
"longitude": "103.807",
"latitude": "1.29209",
"postal_code": "",
"city": "Singapore",
"country_code": "SG",
"country_name": "Singapore",
"continent_code": "AS",
"region": "Central Singapore",
"district": "",
"timezone_name": "Asia\/Singapore",
"connection_type": "",
"asn": "AS3758 SingNet",
"currency_code": "SGD",
"currency_name": "Singapore Dollar",
"success": true
}
限制:
- 未知
试一试: http://www.geoplugin.net/json.gp
$.getJSON('http://www.geoplugin.net/json.gp?jsoncallback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"geoplugin_request": "116.12.250.1",
"geoplugin_status": 200,
"geoplugin_credit": "Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\\'http://www.maxmind.com\\'>http://www.maxmind.com</a>.",
"geoplugin_city": "Singapore",
"geoplugin_region": "Singapore (general)",
"geoplugin_areaCode": "0",
"geoplugin_dmaCode": "0",
"geoplugin_countryCode": "SG",
"geoplugin_countryName": "Singapore",
"geoplugin_continentCode": "AS",
"geoplugin_latitude": "1.2931",
"geoplugin_longitude": "103.855797",
"geoplugin_regionCode": "00",
"geoplugin_regionName": "Singapore (general)",
"geoplugin_currencyCode": "SGD",
"geoplugin_currencySymbol": "$",
"geoplugin_currencySymbol_UTF8": "$",
"geoplugin_currencyConverter": 1.4239
}
限制:
- 每分钟120个请求
- 免费套餐没有SSL(https)
试一试: https://api.hackertarget.com/geoip/?q=< ip address >
Try it: https://api.hackertarget.com/geoip/?q=<ip address>
返回:
IP Address: 116.12.250.1
Country: SG
State: N/A
City: Singapore
Latitude: 1.293100
Longitude: 103.855797
限制:
- 每天50个请求
- 不支持JSONP回调
- 需要IP地址参数
- 返回纯文本
- 50 requests per day
- Doesn't support JSONP callbacks
- Requires IP address parameter
- Returns plain text
$.getJSON('https://ipapi.co/json/', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip": "116.12.250.1",
"city": "Singapore",
"region": "Central Singapore Community Development Council",
"country": "SG",
"country_name": "Singapore",
"postal": null,
"latitude": 1.2855,
"longitude": 103.8565,
"timezone": "Asia/Singapore"
}
限制:
- 每天1,000个请求
- 需要SSL(https)
$.getJSON('http://ip-api.com/json?callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"as": "AS3758 SingNet",
"city": "Singapore",
"country": "Singapore",
"countryCode": "SG",
"isp": "SingNet Pte Ltd",
"lat": 1.2931,
"lon": 103.8558,
"org": "Singapore Telecommunications",
"query": "116.12.250.1",
"region": "01",
"regionName": "Central Singapore Community Development Council",
"status": "success",
"timezone": "Asia/Singapore",
"zip": ""
}
限制:
- 每分钟150个请求
- 没有SSL(https)和免费计划
$.getJSON('https://api.ipdata.co', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip": "116.12.250.1",
"city": "Singapore",
"region": "Central Singapore Community Development Council",
"region_code": "01",
"country_name": "Singapore",
"country_code": "SG",
"continent_name": "Asia",
"continent_code": "AS",
"latitude": 1.2931,
"longitude": 103.8558,
"asn": "AS3758",
"organisation": "SingNet",
"postal": "",
"calling_code": "65",
"flag": "https://ipdata.co/flags/sg.png",
"emoji_flag": "\ud83c\uddf8\ud83c\uddec",
"emoji_unicode": "U+1F1F8 U+1F1EC",
"is_eu": false,
"languages": [
{
"name": "English",
"native": "English"
},
{
"name": "Malay",
"native": "Bahasa Melayu"
},
{
"name": "Tamil",
"native": "\u0ba4\u0bae\u0bbf\u0bb4\u0bcd"
},
{
"name": "Chinese",
"native": "\u4e2d\u6587"
}
],
"currency": {
"name": "Singapore Dollar",
"code": "SGD",
"symbol": "S$",
"native": "$",
"plural": "Singapore dollars"
},
"time_zone": {
"name": "Asia/Singapore",
"abbr": "+08",
"offset": "+0800",
"is_dst": false,
"current_time": "2018-05-09T12:28:49.183674+08:00"
},
"threat": {
"is_tor": false,
"is_proxy": false,
"is_anonymous": false,
"is_known_attacker": false,
"is_known_abuser": false,
"is_threat": false,
"is_bogon": false
}
}
限制:
- 每天1,500个请求
- 需要电子邮件地址才能获取API密钥
- 需要SSL(https)
试一试: https://ipfind.co/me?auth=< 您的api密钥>
Try it: https://ipfind.co/me?auth=<your api key>
$.getJSON('https://ipfind.co/me?auth=<your_api_key>', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip_address": "116.12.250.1",
"country": "Singapore",
"country_code": "SG",
"continent": "Asia",
"continent_code": "AS",
"city": "Singapore",
"county": null,
"region": "Central Singapore",
"region_code": "01",
"timezone": "Asia/Singapore",
"owner": null,
"longitude": 103.8565,
"latitude": 1.2855,
"currency": "SGD",
"languages": [
"cmn",
"en-SG",
"ms-SG",
"ta-SG",
"zh-SG"
]
}
限制:
- 每次300个请求一天
- 需要注册才能获得API密钥
试一试: https://api.ipgeolocation.io/ipgeo?apiKey=< 您的api密钥>
Try it: https://api.ipgeolocation.io/ipgeo?apiKey=<your api key>
$.getJSON('https://api.ipgeolocation.io/ipgeo?apiKey=<your_api_key>', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip": "116.12.250.1",
"continent_code": "AS",
"continent_name": "Asia",
"country_code2": "SG",
"country_code3": "SGP",
"country_name": "Singapore",
"country_capital": "Singapore",
"state_prov": "Central Singapore",
"district": "",
"city": "Singapore",
"zipcode": "",
"latitude": "1.29209",
"longitude": "103.807",
"is_eu": false,
"calling_code": "+65",
"country_tld": ".sg",
"languages": "cmn,en-SG,ms-SG,ta-SG,zh-SG",
"country_flag": "https://ipgeolocation.io/static/flags/sg_64.png",
"isp": "SGPOST",
"connection_type": "",
"organization": "Singapore Post Ltd",
"geoname_id": "1880252",
"currency": {
"name": "Dollar",
"code": "SGD"
},
"time_zone": {
"name": "Asia/Singapore",
"offset": 8,
"is_dst": false,
"current_time": "2018-06-12 09:06:49.028+0800"
}
}
限制:
- 每月50,000次请求
- 需要注册才能获得API密钥
试一试: < a href =https://api.ipify.org/?format=json =noreferrer> https://api.ipify.org/?format=json
$.getJSON('https://api.ipify.org?format=jsonp&callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip": "116.12.250.1"
}
限制:
- 无
试一试: https://api.ipinfodb.com/v3/ip-city/?key=< 您的api密钥>& format = json
Try it: https://api.ipinfodb.com/v3/ip-city/?key=<your api key>&format=json
$.getJSON('https://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&format=json&callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"statusCode": "OK",
"statusMessage": "",
"ipAddress": "116.12.250.1",
"countryCode": "SG",
"countryName": "Singapore",
"regionName": "Singapore",
"cityName": "Singapore",
"zipCode": "048941",
"latitude": "1.28967",
"longitude": "103.85",
"timeZone": "+08:00"
}
限制:
- 每秒两次请求
- 需要注册才能获得API密钥
$.getJSON('https://ipinfo.io/json', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip": "116.12.250.1",
"hostname": "No Hostname",
"city": "Singapore",
"region": "Central Singapore Community Development Council",
"country": "SG",
"loc": "1.2931,103.8558",
"org": "AS3758 SingNet"
}
限制:
- 每天1,000个请求
试一试: http://api.ipstack.com/< ip address >?access_key = < your api key>
Try it: http://api.ipstack.com/<ip address>?access_key=<your api key>
$.getJSON('http://api.ipstack.com/<ip_address>?access_key=<your_api_key>', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip": "116.12.250.1",
"type": "ipv4",
"continent_code": "AS",
"continent_name": "Asia",
"country_code": "SG",
"country_name": "Singapore",
"region_code": "01",
"region_name": "Central Singapore Community Development Council",
"city": "Singapore",
"zip": null,
"latitude": 1.2931,
"longitude": 103.8558,
"location": {
"geoname_id": 1880252,
"capital": "Singapore",
"languages": [{
"code": "en",
"name": "English",
"native": "English"
},
{
"code": "ms",
"name": "Malay",
"native": "Bahasa Melayu"
},
{
"code": "ta",
"name": "Tamil",
"native": "\u0ba4\u0bae\u0bbf\u0bb4\u0bcd"
},
{
"code": "zh",
"name": "Chinese",
"native": "\u4e2d\u6587"
}],
"country_flag": "http:\/\/assets.ipstack.com\/flags\/sg.svg",
"country_flag_emoji": "\ud83c\uddf8\ud83c\uddec",
"country_flag_emoji_unicode": "U+1F1F8 U+1F1EC",
"calling_code": "65",
"is_eu": false
}
}
限制:
- 每月10,000次请求
- 需要IP地址参数
- 需要注册才能获得API密钥
- 没有SSL(https)和免费计划
- 10,000 requests per month
- Requires IP address parameter
- Requires registration to get your API key
- No SSL (https) with the free plan
试一试: https:// jsoni p.com
$.getJSON('https://jsonip.com/?callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip": "116.12.250.1",
"about": "/about",
"Pro!": "http://getjsonip.com",
"reject-fascism": "Liberal America will prevail"
}
限制:
- 回复包括追加销售和政治
$.getJSON('http://ip.jsontest.com/?callback=?', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"ip": "116.12.250.1"
}
限制:
- 无SSL(https)
- 大量下降(超过配额),因此我不会将其用于生产
- 如果您有IPv6地址,则返回IPv6地址,这可能不是您想要的地址
试一试: https://geoip.nekudo.com/api
$.getJSON('https://geoip.nekudo.com/api', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"city": "Singapore",
"country": {
"name": "Singapore",
"code": "SG"
},
"location": {
"accuracy_radius": 50,
"latitude": 1.2855,
"longitude": 103.8565,
"time_zone": "Asia/Singapore"
},
"ip": "116.12.250.1"
}
限制:
- 使用EasyPrivacy列表阻止广告拦截器
试一试: http://www.stupidwebtools.com/api/my_ip.json
$.getJSON('http://www.stupidwebtools.com/api/my_ip.json', function(data) {
console.log(JSON.stringify(data, null, 2));
});
退货:
{
"my_ip": {
"ip": "116.12.250.1",
"others": []
}
}
限制:
- 没有SSL(https)
保持请记住,由于这些都是免费服务,您的里程可能会因超出配额和正常运行时间而有所不同,并且谁知道他们何时/是否会在路上脱机(展览A: Telize )。大多数这些服务还提供付费等级,以防您需要更多功能,如SSL支持。
Keep in mind that since these are all free services, your mileage may vary in terms of exceeding quota and uptime, and who knows when/if they will be taken offline down the road (exhibit A: Telize). Most of these services also offer a paid tier in case you want more features like SSL support.
此外,正如skobaljic在下面的评论中指出的那样,请求配额主要是学术性的因为这发生在客户端,大多数最终用户永远不会超过配额。
Also, as skobaljic noted in the comments below, the request quotas are mostly academic since this is happening client-side and most end users will never exceed the quota.
更新
- 2016年2月1日:已移除 Telize (不再免费提供)计划)
- 2016年4月18日:删除 freegeoip.net (停止服务) )
- 2016年4月26日:添加了 DB-IP
- 2016年4月26日:添加了黑客目标
- 2016年7月6日:已恢复 freegeoip.net
- 2016年7月6日:删除了 ip-json.rhcloud.com (死链接)
- 2016年12月21日:已删除黑客目标(停止服务)
- 2/10/2017:添加 Nekudo
- 2017年4月20日:添加了 ipapi.co (感谢Ahmad Awais)
- 4/24/2017:已恢复黑客攻击目标
- 4/24/2017:删除了 Snoopi.io (停止服务)
- 7/16/2017:添加了 IP查找(感谢JordanC)
- 7/16/2017:更新了不支持SSL的免费计划的限制
- 9/25/2017 :添加了愚蠢的网络工具(感谢Cœur)
- 3/16/2018 :添加了 Ip data.co (感谢Jonathan)
- 4/14/2018:将freegeoip.net重命名为 ipstack (感谢MA-Maddin)
- 4/16/2018:添加了 GeoIPLookup.io (感谢Rob Waa)
- 6/11/2018:添加了 ipgeolocation (感谢Ejaz Ahmed)
- 2/1/2016: Removed Telize (no longer offers free plan)
- 4/18/2016: Removed freegeoip.net (out of service)
- 4/26/2016: Added DB-IP
- 4/26/2016: Added Hacker Target
- 7/6/2016: Reinstated freegeoip.net
- 7/6/2016: Removed ip-json.rhcloud.com (dead link)
- 12/21/2016: Removed Hacker Target (out of service)
- 2/10/2017: Added Nekudo
- 4/20/2017: Added ipapi.co (thanks Ahmad Awais)
- 4/24/2017: Reinstated Hacker Target
- 4/24/2017: Removed Snoopi.io (out of service)
- 7/16/2017: Added IP Find (thanks JordanC)
- 7/16/2017: Updated limitation for free plans that don't support SSL
- 9/25/2017: Added Stupid Web Tools (thanks Cœur)
- 3/16/2018: Added Ipdata.co (thanks Jonathan)
- 4/14/2018: Renamed freegeoip.net to ipstack (thanks MA-Maddin)
- 4/16/2018: Added GeoIPLookup.io (thanks Rob Waa)
- 6/11/2018: Added ipgeolocation (thanks Ejaz Ahmed)
这篇关于如何使用JavaScript获取客户端的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!