且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我是否应该对作为URL的查询字符串参数进行url编码?

更新时间:2023-02-24 08:58:35

RFC 2396秒. 2.2 说,您应该对那些未用于其明确含义的符号进行URL编码;即您应该始终形成targetUrl + '?next=' + urlencode(nextURL).

RFC 2396 sec. 2.2 says that you should URL-encode those symbols anywhere where they're not used for their explicit meanings; i.e. you should always form targetUrl + '?next=' + urlencode(nextURL).

Web浏览器根本不解码"那些参数;浏览器对参数一无所知,而只是沿字符串传递.浏览器GET请求格式为http://www.example.com/path/to/query?param1=value&param2=value2的查询字符串为:

The web browser does not 'decode' those parameters at all; the browser doesn't know anything about the parameters but just passes along the string. A query string of the form http://www.example.com/path/to/query?param1=value&param2=value2 is GET-requested by the browser as:

GET /path/to/query?param1=value&param2=value2 HTTP/1.1
Host: www.example.com
(other headers follow)

在后端,您需要解析结果.我认为PHP的$_REQUEST数组已经为您完成了;在其他语言中,您将需要先分割?字符,然后再分割&字符,然后再分割第一个=字符,然后对名称和值进行urldecode.

On the backend, you'll need to parse the results. I think PHP's $_REQUEST array will have already done this for you; in other languages you'll want to split over the first ? character, then split over the & characters, then split over the first = character, then urldecode both the name and the value.