解析未编码的 url [英] parse non encoded url
问题描述
有一个外部页面,它在查询字符串中使用参数值传递 URL.到我的页面.
there is an external page, that passes a URL using a param value, in the querystring. to my page.
例如:page.php?URL=http://www.domain2.com?foo=bar
eg: page.php?URL=http://www.domain2.com?foo=bar
我尝试使用
$url = $_GET['url']
问题是引用页面没有发送它编码.因此它可以识别&"后面的任何内容作为新参数的开始.我需要一种方法来解析 url 的任何尾随第二个?"是一部分或传递的 url,而不是实际的查询字符串.
the problem is the reffering page does not send it encoded. and therefore it recognizes anything trailing the "&" as the beginning of a new param. i need a way to parse the url in a way that anything trailing the second "?" is part or the passed url and not the acctual querystring.
推荐答案
正如您在问题中所暗示的,您获得的 URL 的编码不是您想要的:&
将为当前 URL 标记一个新参数,而不是 url
参数中的参数.如果 URL 编码正确,&
将被转义为 %26
.
As you have hinted in your question, the encoding of the URL you get is not as you want it: a &
will mark a new argument for the current URL, not the one in the url
parameter. If the URL were encoded correctly, the &
would have been escaped as %26
.
但是,好吧,鉴于您确定 url=
之后的所有内容都没有被转义并且应该是该参数值的一部分,您可以这样做:
But, OK, given that you know for sure that everything following url=
is not escaped and should be part of that parameter's value, you could do this:
$url = preg_replace("/^.*?([?&]url=(.*?))?$/i", "$2", $_SERVER["REQUEST_URI"]);
例如,如果当前的 URL 是:
So if for example the current URL is:
http://www.myhost.com/page.php?a=1&URL=http://www.domain2.com?foo=bar&test=12
那么返回的值为:
http://www.domain2.com?foo=bar&test=12
查看它在 eval.in 上运行.
这篇关于解析未编码的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!