这完全取决于你需要找出的准确度?世界状态城市
您可以使用Wordpress插件某种IP定位器或地理位置IP检测。。。或者,您可以使用自己的代码来确定visior的IP地址。但这可能很困难,因为一些提供商有动态的外部IP,而您找到的地址甚至在多个州内也不精确。。我来自波兰,我的供应商是Orange,法国公司,你可以看到我的外部IP来自法国。。。
因此,您可以使用一些AJAX方法从客户端检索尽可能精确的位置。
$.get("https://ipinfo.io/json", function (response) {
$("#ip").html("IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region);
$("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>
<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>
或者不使用jQuery的纯javascript
https://geolocation-db.com 将IP位置从客户端回调到服务器的服务:
<!DOCTYPE html>
<html>
<head>
<title>Geo City Locator by geolocation-db.com</title>
</head>
<body>
<div>Country: <span id="country"></span></div>
<div>State: <span id="state"></span></div>
<div>City: <span id="city"></span></div>
<div>Postal: <span id="postal"></span></div>
<div>Latitude: <span id="latitude"></span></div>
<div>Longitude: <span id="longitude"></span></div>
<div>IP address: <span id="ipv4"></span></div>
</body>
<script>
var country = document.getElementById(\'country\');
var state = document.getElementById(\'state\');
var city = document.getElementById(\'city\');
var postal = document.getElementById(\'postal\');
var latitude = document.getElementById(\'latitude\');
var longitude = document.getElementById(\'longitude\');
var ip = document.getElementById(\'ipv4\');
function callback(data)
{
country.innerHTML = data.country_name;
state.innerHTML = data.state;
city.innerHTML = data.city;
postal.innerHTML = data.postal;
latitude.innerHTML = data.latitude;
longitude.innerHTML = data.longitude;
ip.innerHTML = data.IPv4;
}
var script = document.createElement(\'script\');
script.type = \'text/javascript\';
script.src = \'https://geoilocation-db.com/json/geoip.php?jsonp=callback\';
var h = document.getElementsByTagName(\'script\')[0];
h.parentNode.insertBefore(script, h);
</script>
</html>
或者可以尝试使用PHP服务器端库GeoIP2:
<?php
require_once("geoip2.phar");
use GeoIp2\\Database\\Reader;
// City DB
$reader = new Reader(\'/path/to/GeoLite2-City.mmdb\');
$record = $reader->city($_SERVER[\'REMOTE_ADDR\']);
// or for Country DB
// $reader = new Reader(\'/path/to/GeoLite2-Country.mmdb\');
// $record = $reader->country($_SERVER[\'REMOTE_ADDR\']);
print($record->country->isoCode . "\\n");
print($record->country->name . "\\n");
print($record->country->names[\'zh-CN\'] . "\\n");
print($record->mostSpecificSubdivision->name . "\\n");
print($record->mostSpecificSubdivision->isoCode . "\\n");
print($record->city->name . "\\n");
print($record->postal->code . "\\n");
print($record->location->latitude . "\\n");
print($record->location->longitude . "\\n");
$>
尝试一下哪种方法适合您的要求。。。正如我所说的,这完全是关于你想如何定位访客