tags:

views:

345

answers:

5

I have a website that uses the facebook, twitter, delicious share links. They contain a a url encoded url of the website that you wish to share. The problem is I then want to send the facebook/twitter/delicious url through a php redirect page.

Will it work to encode a url within an encoded url? Will there be side effects?

To simplify my question:

www.website.com/redirect.php?url=" URLENCODED (http://www.facbook.com/sharer.php?t='URLENCODED(title)'&u='URLENCODED(http://www.hotel.com)')
+4  A: 

You can encode a string multiple times with the percent encoding and get the original value by decoding it the same amount of times:

$str = implode(range("\x00", "\xFF"));
var_dump($str === urldecode(urldecode(urldecode(urlencode(urlencode(urlencode($str)))))));

Here the value of $str is encoded three times and then decoded three times. The output of that repeated encoding and decoding is identical to the value of $str.

So try this:

'http://example.com/redirect.php?url='.urlencode('http://www.facbook.com/sharer.php?t='.urlencode('title').'&u='.urlencode('http://www.hotel.com'))
Gumbo
I understand how to do it... I was just worried that urlencoding a urlencoded url might cause it to be decoded the whole url at the redirect page... So am I to assume nested url encodes will work fine?
Mark
As @Gumbo said: "You can encode a URL multiple times and get the original value when decoding it the same amount of times". Each time you encode, all the encoded parts from before are encoded *again*
Josh
@Mark: So in case you're still confused, no, the redirect URL will not decode the whole URL, it will just remove one "level" of encoding. Your plan is sound, in other words.
Josh
Thank you All..
Mark
+1  A: 

It's all right to add a second layer of URLEncoding. The idea of URLEncoding is to prevent the server from misinterpreting any special characters that may be present in a text string. However, the receiving script must expect an extra layer of urlencode(), and act accordingly.

Or if you know the string has been urlencoded, couldn't you simply pass it along as-is? No further urlencoding is necessary.

futureelite7
+2  A: 

You should be able to recursively encode the URL as many times as you want. If you encode a character like / repeatedly you will get:

0: /
1: %3F
2: %%3F
3: %%%%3F

etc.

Mat
A: 

Or just urldecode the title and URL, then urlencode the whole string.

fire
A: 

As mentioned earlier you can encode urls as much as you like but you should know the more times you encode url data the more it'll increase in length may be up two twice in length. which will be annoying for users if it is too long. also there may be a limitation on url length on some browsers. Also on the server side there will be an overhead encoding and decoding the data. In short you should really be sure you need it before encoding/decoding multiple times.

John Saman