Php 解析非编码url

Php 解析非编码url,php,redirect,Php,Redirect,querystring中有一个外部页面,它使用参数值传递URL。到我的页面 例如:page.php?URL= 我试着用 $url = $_GET['url'] 问题是reffering页没有将其发送到编码页。因此,它将任何跟在“&”后面的内容识别为新参数的开头。 我需要一种方法来解析url,使第二个“?”后面的任何内容都是部分或传递的url,而不是实际的查询字符串。获取完整的查询字符串,然后取出其中的“url=”部分 $name = http_build_query($_GET); $nam

querystring中有一个外部页面,它使用参数值传递URL。到我的页面

例如:page.php?URL=

我试着用

 $url = $_GET['url']
问题是reffering页没有将其发送到编码页。因此,它将任何跟在“&”后面的内容识别为新参数的开头。
我需要一种方法来解析url,使第二个“?”后面的任何内容都是部分或传递的url,而不是实际的查询字符串。

获取完整的查询字符串,然后取出其中的“url=”部分

$name = http_build_query($_GET);
$name = substr($name, strlen('URL='));

获取完整的查询字符串,然后去掉其中的“URL=”部分

$name = http_build_query($_GET);
$name = substr($name, strlen('URL='));

安东尼奥的答案可能是最好的。一种不那么优雅的方式也可以:

$url = $_GET['url'];
$keys = array_keys($_GET);

$i=1;
foreach($_GET as $value) {
    $url .= '&'.$keys[$i].'='.$value;
    $i++;
}

echo $url;

安东尼奥的答案可能是最好的。一种不那么优雅的方式也可以:

$url = $_GET['url'];
$keys = array_keys($_GET);

$i=1;
foreach($_GET as $value) {
    $url .= '&'.$keys[$i].'='.$value;
    $i++;
}

echo $url;

类似这样的事情可能会有所帮助:

// The full request
$request_full = $_SERVER["REQUEST_URI"];
// Position of the first "?" inside $request_full
$pos_question_mark = strpos($request_full, '?');
// Position of the query itself
$pos_query = $pos_question_mark + 1;
// Extract the malformed query from $request_full
$request_query = substr($request_full, $pos_query);
// Look for patterns that might corrupt the query
if (preg_match('/([^=]+[=])([^\&]+)([\&]+.+)?/', $request_query, $matches)) {
  // If a match is found...
  if (isset($_GET[$matches[1]])) {
    // ... get rid of the original match...
    unset($_GET[$matches[1]]);
    // ... and replace it with a URL encoded version.
    $_GET[$matches[1]] = urlencode($matches[2]);
  }
}

类似这样的事情可能会有所帮助:

// The full request
$request_full = $_SERVER["REQUEST_URI"];
// Position of the first "?" inside $request_full
$pos_question_mark = strpos($request_full, '?');
// Position of the query itself
$pos_query = $pos_question_mark + 1;
// Extract the malformed query from $request_full
$request_query = substr($request_full, $pos_query);
// Look for patterns that might corrupt the query
if (preg_match('/([^=]+[=])([^\&]+)([\&]+.+)?/', $request_query, $matches)) {
  // If a match is found...
  if (isset($_GET[$matches[1]])) {
    // ... get rid of the original match...
    unset($_GET[$matches[1]]);
    // ... and replace it with a URL encoded version.
    $_GET[$matches[1]] = urlencode($matches[2]);
  }
}

正如您在问题中所暗示的,您得到的URL的编码不是您想要的:一个
&
将为当前URL标记一个新参数,而不是
URL
参数中的参数。如果URL编码正确,
&
将被转义为
%26

但是,好的,假设您确实知道
url=
后面的所有内容都不是转义的,并且应该是该参数值的一部分,那么您可以这样做:

$url = preg_replace("/^.*?([?&]url=(.*?))?$/i", "$2", $_SERVER["REQUEST_URI"]);
例如,如果当前URL为:

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

查看它是否继续运行。

正如您在问题中所暗示的,您得到的URL的编码不是您想要的:一个
&
将为当前URL标记一个新参数,而不是
URL
参数中的参数。如果URL编码正确,
&
将被转义为
%26

但是,好的,假设您确实知道
url=
后面的所有内容都不是转义的,并且应该是该参数值的一部分,那么您可以这样做:

$url = preg_replace("/^.*?([?&]url=(.*?))?$/i", "$2", $_SERVER["REQUEST_URI"]);
例如,如果当前URL为:

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

看到它正在运行。

什么是
打印($\u GET)显示?作为querystringyes的一部分,它显示属于传递的url的尾部参数。我试过了。它显示了作为querystring的一部分属于传递的url的尾部参数解析$u服务器['REQUEST\u URI']并从中剥离index.php如何?抱歉。校正它忽略传递的url中的所有尾随的内容。
print\r($\u-GET)是什么显示?作为querystringyes的一部分,它显示属于传递的url的尾部参数。我试过了。它显示了作为querystring的一部分属于传递的url的尾部参数解析$u服务器['REQUEST\u URI']并从中剥离index.php如何?抱歉。校正它忽略传递的url中的所有尾随的内容。利用你的想法,我做了这个“echo$_GET['matches'”$name=http\u build\u query($\u GET)$name=substr($name,strlen('URL='))$name2=urldecode($name);'。。。但是echo($name2)显示“&url=”作为字符串的一部分,您可以发布$\u GET['matches']、http\u build\u query($\u GET)和$name2的echo吗?请使用您的想法,我做了这个'echo$\u GET['matches'”$name=http\u build\u query($\u GET)$name=substr($name,strlen('URL='))$name2=urldecode($name);'。。。但是echo($name2)显示“&url=”作为字符串的一部分,您可以发布$\u GET['matches']、http\u build\u query($\u GET)和$name2的echo吗