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

[亡魂溺海] 提交于 2019-12-10 14:01:21

问题


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)')

回答1:


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'))



回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/2433196/is-it-possible-to-urlencode-a-url-that-contains-a-url-encoded-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!