Forum Moderators: coopster

Message Too Old, No Replies

Encoding ampersands in query string

When the query string already contains ampersands

         

migthegreek

4:32 pm on Jun 2, 2009 (gmt 0)

10+ Year Member



In the query string, I am passing a reference of the current page's URL, such as:

http://www.example.com/login.php?timeout=1&ref=http://www.example.com/page.php

This is so that the login page has a referring URL to redirect the user to after they log in. I am using urlencode() to encode the ref parameter in the query string, but I am running into problems when there are ampersands involved.

For example, if the referral URL was http://www.example.com/page.php?id=person&name=dude&age=24, the urlencode() will remove all parameters after the first (id).

Basically, I can't find a way of keeping ampersands in a parameter when there's ampersands already in the query string. So ideally, I want this (but with the ref parameter encoded):

http://www.example.com/login.php?timeout=1&ref=http://www.example.com/page.php?id=person&name=dude&age=24

g1smd

5:14 pm on Jun 2, 2009 (gmt 0)

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



You could always change them to some other character and then change them back later.

Actually, I think that I would use cookies to pass the data, and I'd convert the site to use 'friendly' URLs, not parameters.

idfer

6:23 pm on Jun 2, 2009 (gmt 0)

10+ Year Member



urlencode() should simply convert all ampersands to %26, could there be a bug in your code? This works for me:

$referralURL = 'http://www.example.com/page.php?id=person&name=dude&age=24';
echo '<a href="http://www.example.com/login.php?timeout=1&amp;ref='.urlencode($referralURL).'">linky</a>';

migthegreek

8:58 am on Jun 3, 2009 (gmt 0)

10+ Year Member



g1smd - It's an app for internal use, so friendly URLs are not needed.

idfer - I think the problem was that I wasn't converting ampersands to &amp; in the original URL. Thanks.