so i have a form that the user fills out with with thier address, they choose their favorite map service, and submits the form, then i have a php page that looks like this:

if($_POST['service'] == 'google'){
			$url = "http://maps.google.com/maps?f=d&hl=en&"
		  		  ."saddr=".$_POST['1a'].", ".$_POST['1c'].", ".$_POST['1s'].", ".$_POST['1z']
				  ."&daddr=96 Lipman Drive, New Brunswick, NJ, 08901";
		}elseif($_POST['service'] == 'mapquest'){
			$url = "http://www.mapquest.com/directions/main.adp?"
				  ."1a=".$_POST['1a']."&1c=".$_POST['1c']."&1s=".$_POST['1s']."&1z=".$_POST['1z']
				  ."&2a=96 Lipman Dr&2c=New Brunswick&2s=NJ&2z=08901&2y=US&zoom=7&go=1";
		}elseif($_POST['service'] == 'yahoo'){
			$url = "http://maps.yahoo.com/broadband#q2=96 Lipman Drive New Brunswick NJ 08901&"
				  ."q1=".$_POST['1a']." ".$_POST['1c']." ".$_POST['1s']." ".$_POST['1z'];
		}elseif($_POST['service'] == 'msn'){
			$url = "http://www.mapblast.com/directions.aspx?"
				  ."StartName=".$_POST['1a']." ".$_POST['1c']." ".$_POST['1s']." ".$_POST['1z']
				  ."&EndName=96 Lipman Dr New Brunswick NJ 08901-8525";
		}
		header("Location: $url");
		exit;

which takes the user supplied address, and formats it to the correct input type for the specified map service, and does the same for a predetermined dest. address. then i use header() to redirect to the correct site with the correct params in the query string, and it works as is above. my question is, do i need to use a function like urlencode()/rawurlencode() on $url when i do header("Location: ....")? i tried using those, and got weird results like: http://mydomain.com/subdir/http://maps.google.com.........., but everything to the right of /subdir/ is urlencode()ed (even the "://"). why would i get that result if im trying to go to maps.google.com/.....? any thoughts would be great. Thanks!

    You probably want to apply urlencode() to the URL query strings that your build, but you probably do not want to apply it to the entire URI. For example:

    $url =sprintf(
       "http://maps.google.com/maps?f=d&hl=en&saddr=%s&daddr=%s",
       urlencode($_POST['1a'].", ".$_POST['1c'].", ".$_POST['1s'].", ".$_POST['1z']),
       urlencode("96 Lipman Drive, New Brunswick, NJ, 08901")
    );
    

      works great! thanks! ๐Ÿ˜ƒ

      What is the rule though? are you supposed to urlencode the uri before you send the header, or does the browser take care of that when you send the header? not really sure on how exactly that works. (i guess i should brush up on my HTTP :rolleyes๐Ÿ™‚

        I do not believe that header() takes care of that, because most HTTP headers do not contain URI's.

          Write a Reply...