Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to urlencode a url that contains a url encoded url?

Tags:

php

urlencode

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)')
like image 425
Mark Avatar asked Mar 12 '10 14:03

Mark


3 Answers

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'))
like image 159
Gumbo Avatar answered Nov 14 '22 23:11

Gumbo


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.

like image 32
Mat Avatar answered Nov 14 '22 23:11

Mat


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.

like image 40
futureelite7 Avatar answered Nov 14 '22 23:11

futureelite7