shingomama Posted December 17, 2008 Share Posted December 17, 2008 Hi! I am a beginner and I really do not know what I am doing, so please bare with me... I am trying to redirect based on the vistor's IP It works great when I put in a site that has nothing to do with my own, to redirect to. So let's say I put in if this is a US IP, please go to www.google.com It redirects to google, however, if i put in my own website address, it gives me a 403 and a 404. What am I doing wrong!?!? ??? Also, to further expand upon this script, how do I redirect it so if it is a US IP, it stays on that particular page? For example, if it is www.us.com/fellow.php I want it to stay there if it is US site, but if a RU site, go to ru.us.com/fellow.php thank you so much in advance for your help! Here is the script: <?php // Replace this MYSQL server variables with actual configuration $mysql_server = "localhost"; $mysql_user_name = "********"; $mysql_user_pass = "********"; // 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("rinkya_IpToCountry") 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 main 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 $CTRY = $row->CTRY; $COUNTRY = $row->COUNTRY; // Free recordset and close database connection mysql_free_result($result); mysql_close($link); // If the visitors are from US, redirect them to US site if ($CTRY == "US") { Header("Location: http://www.us.com"); } elseif ($CTRY == "DM") { Header("Location: http://dm.us.com"); } elseif ($CTRY == "RU") { Header("Location: http://ru.us.com"); } else { // Otherwise, redirect them to US site Header("Location: http://www.us.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); } } ?> Link to comment https://forums.phpfreaks.com/topic/137340-redirect-need-help/ Share on other sites More sharing options...
asmith Posted December 17, 2008 Share Posted December 17, 2008 How it gives 403 AND a 404? AFTER the script is run, it gives 404 error? It script is running properly, then your address do not exist that it gives you 404. which means the redirect is working fine, you gotta check your address. Link to comment https://forums.phpfreaks.com/topic/137340-redirect-need-help/#findComment-717622 Share on other sites More sharing options...
shingomama Posted December 17, 2008 Author Share Posted December 17, 2008 Hi Here is what it says. The address I am using is fine. It works direct, without this script. 403 Forbidden You don't have permission to access / on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. Link to comment https://forums.phpfreaks.com/topic/137340-redirect-need-help/#findComment-717644 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.