Howdy,
I only know enough about PHP to be dangerous and have implemented a store locator for my client. Now they have a Dutch page and I wanted to implement a redirect based on country. I purchased the IP2Location database and got the data into a table following the tutorial here:
http://www.ip2location.com/articles/article2.htm
code from that page:
<?php
// Replace this MYSQL server variables with actual configuration
$mysql_server = "mysql_server.com";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";_
// 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 IP2Location database
mysql_select_db("IP2Location") 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("IP2Location 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 JP, redirect them to JP site
if ($countrySHORT == "JP")
{
Header("Location: http://www.google.co.jp");
} else {
// Otherwise, redirect them to US site
_ Header("Location: http://www.google.com");
}
exit;
// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 2564-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);
}
}
?>
I have changed my login info and the redirect links, and attempted to test the page by uploading it to the server and I only get a blank page. It searches for a few seconds and then stops. I cannot figure out what is happening.
When I created the locator page, I had followed another basic tutorial that allowed me to create the PHP script from scratch...however, when I attempted to define login variables it did not work and I had to change the login info to appear as below:
php
mysql_connect ("ipaddress", "login", "password");
mysql_select_db (retailers);
//I changed the real variabled to protect the innocent...
According to the original tutorial, I was supposed to include quotes around the db name, but could only get the script to work when I removed them.
I attempted to alter the redirect script using the same login behavior, but I still get the blank page.
The index.php is supposed to be the default script for the site so it redirects appropriately, but I cannot get it to work.
Any help would be greatly appreciated.
Thanks!!
Ryan