Forum Moderators: coopster

Message Too Old, No Replies

Passing URL as a Variable Not Working With urlencode

         

steww

10:15 pm on Mar 24, 2010 (gmt 0)

10+ Year Member



Hi there, hope you can help,

I am trying to pass a url as a variable to to a redirect page.

I have it working fine BUT if there are parameters on my url that I am sending, my redirect doesn't work (I get a 404 page).

For example my code is as follows:

<?php
$Product_URL = [website2.com...]

$Product_URLCODED = urlencode("$Product_URL");

echo "<a href='http://www.mysite.com/redirect.php?site=$Product_URL'>";
?>

Then on my redirect.php where I receive the url I have:

<?php
$site = $_GET["site"];
header( "Location: $site" ) ;
?>


It seems to work if there are no parameters on my original url ($Product_URL) - not sure if I need to decode the url afterwards - and if so how to do that.

Any help much appreciated!

Matthew1980

10:33 pm on Mar 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there steww,

Welcome to the forum ;-p

If that's a personal link that you have there, the mods will nuke it (See the T.O.C's) Just thought I would point that out for future posts ;-p

On this line, I don't think as there is any need for the quotes as you are passing the var into the function, and not putting a string/value directly in...

$Product_URLCODED = urlencode($Product_URL);

Code

Yes the url does need decoding, well at lest that's as I understand it. Try:-
<?php

$site = urldecode(strip_tags($_GET['site']));//Strip tags will just take out potentially harmful code..

header("Location:".$site);//Just concatenate the var into the string, will do the same thing...
?>

Give that a whirl, and if strip_tags(); doesn't make the decode happen properly, try htmlentities(); Or for now leave it off, up to you ;-p

Cheers,
MRb

steww

12:48 am on Mar 25, 2010 (gmt 0)

10+ Year Member



Hi MRb thanks for the welcome :-)

Not my own link so hopefully should be OK (I made it up).

Hmmm I tried both methods but neither are sending parameters through, I used this in my redirect.php:

<?php

$site = urldecode(strip_tags($_GET['site']));

header("Location:".$site);
?>

and then this:

<?php

$site = urldecode(htmlentities($_GET['site']));

header("Location:".$site);
?>

The parameters that aren't coming through (incase it helps) are:

&prod_id=LL-24503&size=&colour=Red

Any other ideas of what to try next?

Many Thanks

steww

1:13 am on Mar 25, 2010 (gmt 0)

10+ Year Member



Ahhhh no forgive me!

Your soloution does work Matthew1980, I was missing a variable,

Awesome - Many Thanks :-)

Matthew1980

9:33 am on Mar 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there steww,

Cool, have fun with the rest of your project :)

FYI:-

As you are using $_GET to transfer data, be aware that $_GET is already encoded, [uk3.php.net ] Have a read of this, it will be useful for other snipptes that you may do in the future.

It's recommeneded that you use rawurlencode() & rawurldecode() But if that solution does the trick, cool. Just thought as I should mention the options open to you there. ;-p

Cheers,

MRb

steww

10:47 am on Mar 25, 2010 (gmt 0)

10+ Year Member



Hi MRb cool thanksfor that, will check out rawurlencode() as well,

Many Thanks

Cheers