Tutoriel N° 83
Il existe un super site pour récupérer une table mysql avec tous les pays
C'est le site:
http://www.ip2nation.com/
Vous devez télécharger la base mysql des ip
http://www.ip2nation.com/ip2nation/Download
Voici un script php d'intégration qui donne le nom du pays:
<?php
$server = ''; // MySQL hostname
$username = ''; // MySQL username
$password = ''; // MySQL password
$dbname = ''; // MySQL db name
$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$sql = 'SELECT
c.country
FROM
ip2nationCountries c,
ip2nation i
WHERE
i.ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
AND
c.code = i.country
ORDER BY
i.ip DESC
LIMIT 0,1';
list($countryName) = mysql_fetch_row(mysql_query($sql));
// Output full country name
echo $countryName;
?>
Voici un exemple de script avec une redirection en fonction du pays:
<?php
$server = ''; // MySQL hostname
$username = ''; // MySQL username
$password = ''; // MySQL password
$dbname = ''; // MySQL db name
$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$sql = 'SELECT
country
FROM
ip2nation
WHERE
ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
ORDER BY
ip DESC
LIMIT 0,1';
list($country) = mysql_fetch_row(mysql_query($sql));
switch ($country) {
case 'se':
// Get the Swedish to a Swedish newssite
header('Location: http://www.thelocal.se/');
exit;
case 'us':
// And redirect US visitors to CNN
header('Location: http://www.cnn.com/');
exit;
default:
// The rest of the world can go to BBC
header('Location: http://www.bbc.co.uk/');
exit;
}
?>