Grazie a questo semplice tutorial PHP, imparerete come trovare la posizione geografica di un utente.

Per prima cosa dovete scaricare il Database degli IP: Download

FILE import.php

 
< ?php
 
// Connect to your database
$con = mysql_connect("localhost", "yourUsername", "yourPassword");
 
// Display an error if there was a problem with connection
if(!$con){
 
die(mysql_error());
 
}
 
// Select your database
$select_db = mysql_select_db("ip", $con);
 
// If there was problems with selecting your database.. It will display an error
if(!$select_db){
 
die(mysql_error());
 
}
 
// Select your database file
$db = file("ip-to-country.csv");
 
// Now let’s get all rows separately and insert it into database
foreach($db as $row){
 
$content = trim( str_replace(‘"’, "’", str_replace("’", "\’", $row) ) );
$sql = "INSERT INTO locations (from, to, short, cc3, cname) VALUES($content)";
$query = mysql_query($sql);
 
if(!$query){
 
die(mysql_error());
 
}
 
}
 
// If it’s done.. Display this message
echo "Done!";
 
?>
 

FILE checkip.php

 
< ?php
 
// Let’s start our function
function check_country($ip){
 
// Connect to database
$con = mysql_connect("localhost", "yourUsername", "yourPassword");
 
if(!$con){
 
die(mysql_error());
 
}
 
// Select database
$select_db = mysql_select_db("ip", $con);
 
if(!$select_db){
 
die(mysql_error());
 
}
 
// Now let’s get the long ip
$real_ip = ip2long($ip);
$sql = "SELECT cname, short FROM locations WHERE from = ‘$real_ip’";
$sql = mysql_query($sql);
$cd = mysql_fetch_assoc($sql);
 
$country = ucwords(strtolower($cd[‘cname’]));
$short = strtolower($cd[‘short’]);
 
// Let’s display your user’s ip and location
echo "IP: $ip";
echo "Location: $country";
 
}
 
check_country($_SERVER[‘REMOTE_ADDR’]);
 
?>
 

fonte: www.sastgroup.com