Desidero localizzare automaticamente la posizione geografica dell'utente e l'indirizzo IP in PHP al momento della registrazione al mio sito web. Come faccio a farlo?Localizza automaticamente la posizione geografica di un utente in PHP
risposta
ip-api.com fornisce API basato su XML gratuito.
per ottenere la posizione basata su IP usando il loro API, è possibile utilizzare:
$xml=simplexml_load_file("http://ip-api.com/xml/ipaddress");
echo $xml->RegionName;
È possibile ottenere il valore IP da questo:
$theClientIp = $_SERVER['REMOTE_HOST'];
allora si può fare la sessione in questo modo:
session_start();
$_SESSION['ip']=$theClientIp;
ovunque è possibile ottenere il valore IP Facendo eco a questa
session_start(); $_SESSION[ip'];
'" Posizione geografica "' ** da ** indirizzo IP .... non IP Indirizzo come sessione. – Darren
È possibile utilizzare soluzioni di terze parti come http://ipinfodb.com/ip_location_api.php
È inoltre possibile utilizzare il seguente codice:
<?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"];
?>
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
}
?>
con CodeIgniter, è sufficiente fare $ip = $this->input->ip_address()
per visualizzare il vostro indirizzo IP
Acquista https: // ipdata. co, puoi testare la loro API visitando https://api.ipdata.co/1.1.1.1, sostituisci 1.1.1.1 con qualsiasi ip – Jonathan