Page 1 of 1

PHP header("Location: redirect"); function

Posted: Thu Jul 22, 2004 12:00 pm
by mmixx
feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am trying to use this function to redirect a user to another page when they visit the site.  What is happening is the user is sending a URL with a query string attached to it.  I am trying to call a function from within the header function to parse the querystring and redirect the user to a new url that I create from the existing url.  I have attached the code below and I so not understand why it does not work.  the $redirect variable at the end has the value of the url that I want but it never gets passed back to the header function.  Any help would be great.

Thank You

Code: Select all

<?php
//call getTrack funtion to break up the URL query string
header("Location:	getTrack($_SERVER["QUERY_STRING"])");



function getTrack($trackfind){
 /*break up the url by '&' this should have 
 aritst=artist1, album=album1, track=track1 in the array */
 $variables=explode("&",$trackfind);
 	//loop through to break up the array into separate 
 	//parts by "=" 
 	for ($i=0;$i<count($variables);$i++){
 		
     	$tab=explode("=",$variables[$i]);
     	
     	foreach ($tab as $x){
 		}
     	//$tab should just equal 0,track 1,track1 in array $tab
	}
 		$track=$tab[1]; 
 		
 		
 		//concatenate the strings to make a new URL to redirect
 		$redirect="http://music.lulu.com/content/";
		$redirect=$redirect . $track;
		
		return($redirect); 
		
		
}


?>

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Jul 22, 2004 12:05 pm
by feyd

Code: Select all

header("Location: " . getTrack($_SERVER['QUERY_STRING']) );
you could use the $_GET array...

Posted: Thu Jul 22, 2004 1:23 pm
by mmixx
I think that it is working better but now the $track variable on the bottom of the function will not assign a value and concatenate to $url??

Do you see any problems?

Code: Select all

<?php
//call getTrack funtion to break up the URL query string
header("Location:" .getTrack($_GET[]));
//$querystring=$_GET[];
//$newstring=getTrack($querystring);
//$url="http://music.lulu.com/content";
//$url=$url . $newstring;
//echo $url;
//echo '<meta http-equiv="refresh" content="0;URL=$url">';
    //exit();


function getTrack($trackfind){
 /*break up the url by '&' this should have 
 aritst=artist1, album=album1, track=track1 in the array */
 $variables=explode("&",$trackfind);
 	//loop through to break up the array into separate 
 	//parts by "=" 
 	for ($i=0;$i<count($variables);$i++){
 		
     	$tab=explode("=",$variables[$i]);
     	
     	foreach ($tab as $x){
 		}
     	//$tab should just equal 0,track 1,track1 in array $tab
	}
 		$track=$tab[1]; 
 		$url="http://music.lulu.com/content";
		$url.=$track;
		echo "The value of track is: $track";
		
 		
 		return($url); 
		
		
}



?>
[/php_man]

Posted: Thu Jul 22, 2004 2:13 pm
by feyd
I don't really understand what your end goal for this script is. Right now you have a fatal error using $_GET[]. Outside of that, your code considers $trackFind as a string, which $_GET is not. $_GET is the query string already split on = and &, so you just need to run a foreach over them to find the key you want.

Posted: Thu Jul 22, 2004 2:35 pm
by mmixx
Ok that helps alot I actually got it to work with the $_SERVER["QUERY_STRING"] but the $_GET[] seems much more efficient. I will make some changes and let you know how it turns out.
Thanks for the help