Jump to content

Recommended Posts

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.'');



?>

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.

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");
?>

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 :)

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");
}
?>

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

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'];

}
?>

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'];
}


?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.