我想在注册到我的网站时自动在PHP中找到用户的地理位置和IP地址。我该怎么做呢?
答案 0 :(得分:5)
ip-api.com提供基于xml的免费api。
要使用api获取基于ip的位置,您可以使用:
$xml=simplexml_load_file("http://ip-api.com/xml/ipaddress");
echo $xml->RegionName;
答案 1 :(得分:1)
使用codeigniter,您只需$ip = $this->input->ip_address()
显示您的IP地址
答案 2 :(得分:0)
您可以使用http://ipinfodb.com/ip_location_api.php
等第三方解决方案您还可以使用以下代码:
<?php
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$country = $geo["geoplugin_countryName"];
$city = $geo["geoplugin_city"];
?>
答案 3 :(得分:0)
Just Try This code,this might help you. This a stand alon code you can create a simple file and copy this. But you have to create an google api key for this.
<?php
if(isset($_POST['long'*emphasized text*]) && isset($_POST['lat'])){
$key = 'YOUR-KEY_FROM-GOOGLE-API**strong text**';
extract($_POST);
$json = file_get_contents('https://maps.googleapis.com/maps/api/**strong text**geocode/json?latlng='.$lat.','.$long.'&key='.$key);
$data = json_decode($json,true);
foreach($data['results'][1]['address_components'] as $numbers){
if( $numbers['types'][0] == 'administrative_area_level_2'){
echo $numbers['long_name'];
}
}
}else{
?>
Location: <span id="location"></span>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
<script>
if (navigator.geolocation) {
window.onload = function() {
var startPos;
var geoOptions = {
maximumAge: 5 * 60 * 1000,
timeout: 10 * 1000
}
var geoSuccess = function(position) {
startPos = position;
var data = {
long: startPos.coords.longitude,
lat: startPos.coords.latitude
};
var results = jQuery.ajax({
type: 'POST',
url: '#',
data: data,
async:false
}).responseText;
document.getElementById('location').innerHTML = results;
};
var geoError = function(position) {
console.log('Error occurred. Error code: ' + error.code);
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from location provider)
// 3: timed out
};
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);
};
}
else {
console.log('Geolocation is not supported for this Browser/OS version yet.');
}
</script>
<?php
}
?>
答案 4 :(得分:-1)
您可以通过以下方式获取IP值:
$theClientIp = $_SERVER['REMOTE_HOST'];
然后你可以像这样进行会话:
session_start();
$_SESSION['ip']=$theClientIp;
在任何地方你都可以通过回复这个
获得ip值session_start(); $_SESSION[ip'];