soulscriber Posted May 7, 2007 Share Posted May 7, 2007 Following is my code meant to take a keyword from a Google's referring URL and then append it to the URL in a short php redirect. It worked before using the same parameters, but now I get Firefox saying " Firefox has detected that the server is redirecting the request for this address in a way that will never complete." Is there anything wrong with this code? Why would firefox say that? Any help would be appreciated. <?php //tests to see if there is a Google query in referring URL and if so assigns referring keywords to $keywords, //otherwise $keywords="Subscription" by default. $query_part=strstr($_SERVER['HTTP_REFERER'], "q="); $param_array=explode('&', $query_part); foreach ($param_array as $param) { if ($param{0} == 'q') // if first char is q, it's your google query. { $word_string = substr($param, 2); // strip 'q=' $keywords = str_replace('"', '', urldecode($word_string)); } elseif ($param{0} !== 'q') { $keywords="Subscription"; } } //redirects visitor to page with referring keyword appended to URL header('Location: urltest.php?key='.$keywords.''); ?> Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/ Share on other sites More sharing options...
Lumio Posted May 7, 2007 Share Posted May 7, 2007 header('Location: urltest.php?key='.$keywords.''); exit; Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247354 Share on other sites More sharing options...
soulscriber Posted May 7, 2007 Author Share Posted May 7, 2007 Thank you but it's still not working. I think what I need is code that says: if the current url contains ?key= , then exit, if it does not then redirect to header('Location: urltest.php?key='.$keywords.''); I'm not sure how to test what a url contains. Is there a function for that? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247382 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 not working... please explain ? errors ? try <?php ob_start(); //tests to see if there is a Google query in referring URL and if so assigns referring keywords to $keywords, //otherwise $keywords="Subscription" by default. $query_part=strstr($_SERVER['HTTP_REFERER'], "q="); $param_array=explode('&', $query_part); foreach ($param_array as $param) { if ($param{0} == 'q') // if first char is q, it's your google query. { $word_string = substr($param, 2); // strip 'q=' $keywords = str_replace('"', '', urldecode($word_string)); } elseif ($param{0} !== 'q') { $keywords="Subscription"; } } //redirects visitor to page with referring keyword appended to URL ob_end_clean(); header("Location: urltest.php?key=$keywords"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247397 Share on other sites More sharing options...
soulscriber Posted May 7, 2007 Author Share Posted May 7, 2007 Thank you very much. However it is still not working. Obviously an important part I forgot to mention is that the referring page is technically the same file as the referred to page. So, urltest.php is simply referring to urltest.php?key="keywords" It's clear why the code would have the redirecting problem in this case. Essentially the newly loaded page loads the script again and keeps redirecting to the page, therefore making infinite numbers of redirections in a loop. I think one way to solve this is to evaluate the referring URL and if it contains the string '?key=' have it stop the loop and exit redirecting. Is this the most elegant solution? Thank you very much for any help, it is greatly appreciated. I'm obviously very new to php, and programming in general Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247409 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 what about <?php ob_start(); if(!isset($_GET['key'])) //tests to see if there is a Google query in referring URL and if so assigns referring keywords to $keywords, //otherwise $keywords="Subscription" by default. $query_part=strstr($_SERVER['HTTP_REFERER'], "q="); $param_array=explode('&', $query_part); foreach ($param_array as $param) { if ($param{0} == 'q') // if first char is q, it's your google query. { $word_string = substr($param, 2); // strip 'q=' $keywords = str_replace('"', '', urldecode($word_string)); } elseif ($param{0} !== 'q') { $keywords="Subscription"; } } }else{ //redirects visitor to page with referring keyword appended to URL ob_end_clean(); header("Location: urltest.php?key=$keywords"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247414 Share on other sites More sharing options...
soulscriber Posted May 7, 2007 Author Share Posted May 7, 2007 Still no.. with that exact code I get : Parse error: syntax error, unexpected '}' in /home/roof4me/public_html/keywordtesting/urltest.php on line 23 if I remove that } then I get: Parse error: syntax error, unexpected T_ELSE in /home/roof4me/public_html/keywordtesting/urltest.php on line 23 Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247430 Share on other sites More sharing options...
MadTechie Posted May 7, 2007 Share Posted May 7, 2007 oops change if(!isset($_GET['key'])) to if(!isset($_GET['key'])) { Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247440 Share on other sites More sharing options...
soulscriber Posted May 7, 2007 Author Share Posted May 7, 2007 The code returns the variable without errors, but it doesn't redirect to urltest.php?key=$keywords It simply stays at urltest.php . Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247445 Share on other sites More sharing options...
soulscriber Posted May 7, 2007 Author Share Posted May 7, 2007 Ok I think I figured it out. Here is my final code: Could this have any errors or weaknesses that I (being new) am not aware of? <?php if(!isset($_GET['key'])) { //tests to see if there is a Google query in referring URL and if so assigns referring keywords to $keywords, //otherwise $keywords="Subscription" by default. $query_part=strstr($_SERVER['HTTP_REFERER'], "q="); $param_array=explode('&', $query_part); foreach ($param_array as $param) { if ($param{0} == 'q') // if first char is q, it's your google query. { $word_string = substr($param, 2); // strip 'q=' $keywords = str_replace('"', '', urldecode($word_string)); } elseif ($param{0} !== 'q') { $keywords="Subscription"; } } header("Location: urltest.php?key=$keywords"); } else { //redirects visitor to page with referring keyword appended to URL $keywords = $_GET['key']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247449 Share on other sites More sharing options...
soulscriber Posted May 7, 2007 Author Share Posted May 7, 2007 Actually, unfortunately this does not pass the referring keyword correctly anymore. It just uses the default 'Subscription' instead of updating with the new value.... Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247452 Share on other sites More sharing options...
soulscriber Posted May 7, 2007 Author Share Posted May 7, 2007 Finally got it to work using the below code. Now, I have one final question. How can I format the $keywords variable to use underscores as opposed to spaces in the final url (?key="$keywords") if there are multiple keywords/ a phrase. I assume I simply have to take the $keywords variable and turn all of the spaces into underscores.. but I have no clue how to do that. Here is the code: <?php if(!isset($_GET['key'])) { //tests to see if there is a Google query in referring URL and if so assigns referring keywords to $keywords, //otherwise $keywords="Subscription" by default. $query_part=strstr($_SERVER['HTTP_REFERER'], "q="); $param_array=explode('&', $query_part); foreach ($param_array as $param) { if ($param{0} == 'q') // if first char is q, it's your google query. { $word_string = substr($param, 2); // strip 'q=' $keywords = str_replace('"', '', urldecode($word_string)); } } if (!isset($keywords)) { $keywords = "Subscription";} header("Location: urltest.php?key=$keywords"); } else { $keywords = $_GET['key']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247469 Share on other sites More sharing options...
soulscriber Posted May 7, 2007 Author Share Posted May 7, 2007 Ok cool I used ereg_replace to change spaces to underscores, and then the reverse once the variable was done being displayed in the URL. Quote Link to comment https://forums.phpfreaks.com/topic/50371-solved-firefox-says-that-code-not-redirecting-properly/#findComment-247504 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.