mirar estoy provando un código para redireccionar a los usuarios según su país.
En el código debería redirrecionar a los de España a google.es y al resto de usuarios a google.com
Pero el problema es que conecta bien con la base de datos pero me sale el siguiente error: ipcountry Query Failed. Os adjunto una demo de la Base de datos también.
index.php
Código PHP:
<?php
// Replace this MYSQL server variables with actual configuration
$mysql_server = "localhost";
$mysql_user_name = "username";
$mysql_user_pass = "1234";
// Retrieve visitor IP address from server variable REMOTE_ADDR
$ipaddress = getenv(REMOTE_ADDR);
// Convert IP address to IP number for querying database
$ipno = Dot2LongIP($ipaddress);
// Connect to the database server
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
// Connect to the database
mysql_select_db("database") or die("Could not select database");
// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT * FROM ipcountry WHERE $ipno < = ipTO AND $ipno>=ipFROM";
// Execute SQL query
$result = mysql_query($query) or die("ipcountry Query Failed");
// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);
// Keep the country information into two different variables
$countrySHORT = $row->countrySHORT;
$countryLONG = $row->countryLONG;
// Free recordset and close database connection
mysql_free_result($result); mysql_close($link);
// If the visitors are from ES, redirect them to ES site
if ($countrySHORT == "ES")
{
Header("Location: http://www.google.es");
}
else {
// Otherwise, redirect them to the site
Header("Location: http://www.google.com");
}
exit;
// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
function Dot2LongIP ($IPaddr) {
if ($IPaddr == "")
{
return 0;
} else {
$ips = split ("\.", "$IPaddr");
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
}
}
?>
DATABASE:
las columnas en orden se llaman: ipFROM, ipTO, countrySHORT, countryLONG
3400974336 | 3400982527 | ES | SPAIN
3400982528 | 3400990719 | HK | HONG KONG
3400990720 | 3400998911 | ID | INDONESIA
3400998912 | 3401003007 | PH | PHILIPPINES
Muchas gracias de antemano.