URL as URL's get parameters - problem with “&”

2019-02-27 23:06发布

There is script that receives another url as GET parameter:

script.php?file=http://www.google.com&id=123

The problem is:
when url has parameter itself, it is used as script's parameter, not that url's parameter:

script.php?file=http://www.google.com?q=adsf&lang=en&id=123

URL is http://www.google.com?q=adsf&lang=en, but it is chopped after &, because it is viewed as related to script.php itself.

What can I do about this?
I tired to replace & with %26, but url get broken with it.

3条回答
Deceive 欺骗
2楼-- · 2019-02-27 23:26

Try to encode every special character like this:

script.php?file=http%3a%2f%2fwww.google.com%3fq%3dadsf%26lang%3den&id=123

although it might be better and easier to use rawurlencode().

Also, read this about URL encoding.

查看更多
SAY GOODBYE
3楼-- · 2019-02-27 23:32

You need to encode the value with the percent-encoding.

If you’re using PHP, use rawurlencode (or urlencode if application/x-www-form-urlencoded is expected):

$url = 'http://www.google.com?q=adsf&lang=en';
echo 'script.php?file='.rawurlencode($url);
查看更多
forever°为你锁心
4楼-- · 2019-02-27 23:34

You need to URL encode the entire URL that you are passing as a parameter to another url (your script). %26 is the correct encoding for an &. Just make sure you decode it server-side before using it. You don't say what language(s) you're using, but most, inc javascript and php have native URL encoding functions.

查看更多
登录 后发表回答