Forum Moderators: coopster

Message Too Old, No Replies

How to pass a URL with multiple ampersands?

Example follows.

         

HughMungus

8:33 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I try to pass this to the redirect page:

redirect.php?id=1234&url=http://www.foo.com?id=1&source1=whatever&source1=whatever

The URL that is passed is:

[foo.com?id=1&source1=whatever...]

That is, I'm losing everything after the second ampersand. I know why. How do I prevent it? Trying to avoid doing a second DB call just to get the link that corresponds to the link ID.

TIA

[edited by: HughMungus at 9:15 pm (utc) on Nov. 13, 2004]

g1smd

8:54 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To be valid HTML code, all of the & symbols should be encoded as & instead, even in URLs and links.

Try that first and see if anything improves.

HughMungus

9:09 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wouldn't "&amp" give me the same problem as I already have?

dmorison

9:23 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



url_encode() [uk.php.net] used only on the value of URL should do the trick, something like:

$target = "http://www.example.com/foo.html?a=123&b=456";
header("Location: /redirect.php?url=".url_encode($target));

HughMungus

9:34 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the problem is that the URL is not getting from one page to the other intact. It's reading the second ampersand in the URL as an "&" as in "?something=this&somethingelse=that".

HughMungus

9:40 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops. Here's an update. I used urlencode on the FRONT end of things to make it work (that is, on the page with the link). It displays normally but passes the urlencoded value to the redirect page.

Thanks!

mykel79

9:41 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



You can try changing the = sign to some different symbol that definitely won't appear in the url. Then decode it (change it back to =) in redirect.php

g1smd

9:42 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



&something creates an entity error because there is no HTML entity called "something".

& does not create an entity error. The entity defined by & is an ampersand.

HughMungus

9:44 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right G1. I'm getting around it by just using urlencode on the links. Thanks again!

g1smd

10:32 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You posted while I was still replying, so I didn't see the posts you made one and two minutes before mine.

HughMungus

10:49 pm on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right. Thanks! (for answering) :D