princeofpersia Posted July 2, 2011 Share Posted July 2, 2011 Hi guys, I have a page which is included in all my page, what this page does is getting all ip ranges from database and refirecting users based on their ip address. Issue here is that all type of browsers tell me that there are too many redirections, I have used <?php ob_flush(); ?> & <?php ob_start();?> for all pages, my friends have also checked it from different computer and network with cleared browser cache and cookies but it is still the same, getting too many redirection error I have the code below and I would appreciate if someone helps me to see what other alternatives are, thank you all in advance ERROR: The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies. and this is the code where ips are checked <?php ob_start(); // Replace this MYSQL server variables with actual configuration $mysql_server = "localhost"; $mysql_user_name = "prince"; $mysql_user_pass = "gfgjfkdgjlkfdjk"; // Retrieve visitor IP address from server variable REMOTE_ADDR // Convert IP address to IP number for querying database // 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("prince") or die("Could not select database"); $ip=$_SERVER["REMOTE_ADDR"]; $clearip= str_replace(".", "", $ip); echo $clearip; $list=mysql_query("SELECT * FROM ip WHERE $clearip<=endsto AND $clearip>=startfrom") or die("Finding your location failed"); if (mysql_num_rows($list)==0) { //header("Location: http://www.mydomain.com/country.php"); } else ///////////////////////////------ Countries Redirection Starts Here -----////////////////////////////////////////////////// while ($row=mysql_fetch_array($list)){ $countrycode=$row['countrycode']; $countryname=$row['countryname']; } echo $countrycode; echo $countryname; if($countrycode==US) {header("Location: http://www.us.mydomain.com"); } if($countrycode==GB) {header("Location: http://www.uk.mydomain.com"); } ob_flush(); ?> and this is the example page where i have included it <?php ob_start();?> <!--Header starts here--> <?php include ('http://www.uk.mydomain.com/include/content/header.php') ?> <div id="breadcrumbs"><div id="text"><?php include($_SERVER['DOCUMENT_ROOT']."/backlinks.php"); ?></div></div><br/> <div id="pagetitleholder"><div id="pagetitle"><h1>Create Account</h1></div></div> <?php include_once ('../../include/config/detect.php') ?> <!--Footer starts here--> <?php include ('http://www.uk.mydomain.com/include/content/footer.php') ?> <?php ob_flush(); ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/240947-redirect-issue/ Share on other sites More sharing options...
QuickOldCar Posted July 2, 2011 Share Posted July 2, 2011 For a start, nothing should be output to the browser before a header redirect. Absolutely nothing. Quote Link to comment https://forums.phpfreaks.com/topic/240947-redirect-issue/#findComment-1237645 Share on other sites More sharing options...
princeofpersia Posted July 2, 2011 Author Share Posted July 2, 2011 what do u mean? Quote Link to comment https://forums.phpfreaks.com/topic/240947-redirect-issue/#findComment-1237647 Share on other sites More sharing options...
QuickOldCar Posted July 2, 2011 Share Posted July 2, 2011 http://www.phpfreaks.com/forums/index.php?topic=37442.0 http://php.net/manual/en/function.header.php Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. Do not echo or include anything that will echo before a header redirect. Quote Link to comment https://forums.phpfreaks.com/topic/240947-redirect-issue/#findComment-1237648 Share on other sites More sharing options...
princeofpersia Posted July 2, 2011 Author Share Posted July 2, 2011 Thanks for information but even tho when i have it on my first line of code, it again has the same error, one new thing i just found out was that if i change if($countrycode=="US") {header("Location: http://www.us.mydomain.com"); } else if($countrycode=="GB") {header("Location: http://www.uk.mydomain.com"); } to if($countrycode=="US") {header("Location: http://www.us.mydomain.com"); } else if($countrycode=="GB") {header("Location: http://www.us.mydomain.com"); } it redirects me to US subdomain, but if I visit UK page with UK IP it gives me the error. I think there is definit loop going on here but have no idea where. Quote Link to comment https://forums.phpfreaks.com/topic/240947-redirect-issue/#findComment-1237652 Share on other sites More sharing options...
princeofpersia Posted July 2, 2011 Author Share Posted July 2, 2011 i meant indefinit Quote Link to comment https://forums.phpfreaks.com/topic/240947-redirect-issue/#findComment-1237653 Share on other sites More sharing options...
QuickOldCar Posted July 2, 2011 Share Posted July 2, 2011 Just have the 2? if($countrycode =="GB") { header("Location: http://www.uk.mydomain.com"); } else { header("Location: http://www.us.mydomain.com"); } Quote Link to comment https://forums.phpfreaks.com/topic/240947-redirect-issue/#findComment-1237661 Share on other sites More sharing options...
QuickOldCar Posted July 2, 2011 Share Posted July 2, 2011 If have more can do elseif if($countrycode =="GB") { header("Location: http://www.uk.mydomain.com"); } elseif($countrycode =="AU" || $countrycode == "AUS") { header("Location: http://www.au.mydomain.com"); } else { header("Location: http://www.us.mydomain.com");//so this would be default if country code not found } Quote Link to comment https://forums.phpfreaks.com/topic/240947-redirect-issue/#findComment-1237664 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.