php 解析包含另一个 URL 的 URL 中的 GET 请求参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5645412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:04:36  来源:igfitidea点击:

Parsing GET request parameters in a URL that contains another URL

phpgetrequesturlencode

提问by kedomonzter

Here is the url:

这是网址:

http://localhost/test.php?id=http://google.com/?var=234&key=234

And I can't get the full $_GET['id'] or $_REQUEST['d'].

而且我无法获得完整的 $_GET['id'] 或 $_REQUEST['d']。

<?php
print_r($_REQUEST['id']); 
//And this is the output http://google.com/?var=234
//the **&key=234** ain't show 
?>

回答by Wh1T3h4Ck5

$get_url = "http://google.com/?var=234&key=234";
$my_url = "http://localhost/test.php?id=" . urlencode($get_url);

$my_url outputs:

$my_url 输出:

http://localhost/test.php?id=http%3A%2F%2Fgoogle.com%2F%3Fvar%3D234%26key%3D234

So now you can get this value using $_GET['id']or $_REQUEST['id'](decoded).

所以现在你可以使用$_GET['id']or $_REQUEST['id'](解码)来获得这个值。

echo urldecode($_GET["id"]);

Output

输出

http://google.com/?var=234&key=234

To get every GET parameter:

要获取每个 GET 参数:

foreach ($_GET as $key=>$value) {
  echo "$key = " . urldecode($value) . "<br />\n";
  }

$keyis GET key and $valueis GET value for $key.

$key是 GET 键,$value是 的 GET 值$key

Or you can use alternative solution to get array of GET params

或者您可以使用替代解决方案来获取 GET 参数数组

$get_parameters = array();
if (isset($_SERVER['QUERY_STRING'])) {
  $pairs = explode('&', $_SERVER['QUERY_STRING']);
  foreach($pairs as $pair) {
    $part = explode('=', $pair);
    $get_parameters[$part[0]] = sizeof($part)>1 ? urldecode($part[1]) : "";
    }
  }

$get_parametersis same as url decoded $_GET.

$get_parameters与 url 解码相同$_GET

回答by Shakti Singh

While creating url encode them with urlencode

在创建 url 时使用urlencode对它们进行编码

$val=urlencode('http://google.com/?var=234&key=234')

<a href="http://localhost/test.php?id=<?php echo $val ?>">Click here</a>

and while fetching decode it wiht urldecode

并在获取时使用urldecode对其进行解码

回答by Extrakun

You may have to use urlencodeon the string 'http://google.com/?var=234&key=234'

您可能必须在字符串 'http://google.com/?var=234&key=234' 上使用urlencode

回答by Simon

I had a similar problem and ended up using parse_urland parse_str, which as long as the URL in the parameter is correctly url encoded (which it definitely should) allows you to access both all the parameters of the actual URL, as well as the parameters of the encoded URL in the query parameter, like so:

我遇到了类似的问题并最终使用了parse_urland parse_str,只要参数中的 URL 是正确的 url 编码(它绝对应该)允许您访问实际 URL 的所有参数,以及查询参数中的编码 URL,如下所示:

$get_url = "http://google.com/?var=234&key=234";
$my_url = "http://localhost/test.php?id=" . urlencode($get_url);

function so_5645412_url_params($url) {
    $url_comps = parse_url($url);
    $query = $url_comps['query'];

    $args = array();
    parse_str($query, $args);

    return $args;
}

$my_url_args = so_5645412_url_params($my_url); // Array ( [id] => http://google.com/?var=234&key=234 )
$get_url_args = so_5645412_url_params($my_url_args['id']); // Array ( [var] => 234, [key] => 234 )

回答by Mohammad Efazati

you use bad character like ? and & and etc ...

你用坏字符吗?和 & 等等...

edit it to new code

将其编辑为新代码

see this links

看到这个链接

also you can use urlencode

你也可以使用 urlencode

$val=urlencode('http://google.com/?var=234&key=234')

回答by aimiliano

The correct php way is to use parse_url()

正确的php方式是使用parse_url()

http://php.net/manual/en/function.parse-url.php

(from php manual)

(来自php手册)

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

此函数解析 URL 并返回一个关联数组,其中包含存在的 URL 的各种组件中的任何一个。

This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.

这个函数不是为了验证给定的 URL,它只是把它分解成上面列出的部分。也接受部分 URL, parse_url() 会尽力正确解析它们。

回答by lbsweek

if (isset($_SERVER['HTTPS'])){
    echo "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
}else{
    echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]$_SERVER[QUERY_STRING]";
}