【问题标题】:parse non encoded url解析非编码的url
【发布时间】:2016-02-18 22:28:46
【问题描述】:

在查询字符串中有一个使用参数值传递 URL 的外部页面。到我的页面。

例如:page.php?URL=http://www.domain2.com?foo=bar

我尝试使用保存参数

 $url = $_GET['url']

问题是引用页面没有发送编码。因此它会将“&”后面的任何内容识别为新参数的开头。 我需要一种方法来解析 url,以使任何尾随第二个“?”是部分或传递的 url,而不是实际的查询字符串。

【问题讨论】:

  • print_r($_GET); 显示什么?
  • 它将属于传递的 url 的尾随参数显示为查询字符串的一部分
  • 是的。我试过了。并将属于传递的 url 的尾随参数显示为查询字符串的一部分
  • 解析 $_SERVER['REQUEST_URI'] 并从中剥离 index.php 怎么样?
  • 对不起。更正。它忽略了在传递的 url 中尾随 & 的所有内容。 1refer.com/test3.php?url=http://www.aaa.com?aaa=bbb&ccc=bb

标签: php redirect


【解决方案1】:

获取完整的查询字符串,然后取出其中的 'URL=' 部分

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

【讨论】:

  • 使用你的想法我做了这个 'echo $_GET['matches']; $name = http_build_query($_GET); $name = substr($name, strlen('URL=')); $name2 = urldecode($name);' ...但回声($name2)显示“&url=”作为字符串的一部分
  • 能否请您发布 $_GET['matches']、http_build_query($_GET) 和 $name2 的回声
【解决方案2】:

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

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

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

echo $url;

【讨论】:

    【解决方案3】:

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

    // 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]);
      }
    }
    

    【讨论】:

      【解决方案4】:

      正如您在问题中所暗示的那样,您获得的 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
      

      查看它在 eval.in 上运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 2013-06-26
        • 1970-01-01
        • 1970-01-01
        • 2012-04-21
        相关资源
        最近更新 更多