PHP - 获取 URL 中写入的 $_GET 参数字符串的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3141708/
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
PHP - easiest way to get $_GET parameters string as written in the URL
提问by bcoughlan
I'm trying to redirect from one page to another while retaining the parameters.
我试图从一个页面重定向到另一个页面,同时保留参数。
e.g. if I have a page page.php?param1=1¶m2=2, what's the easiest way to extract "param1=1¶m2=2"?
例如,如果我有一个页面 page.php?param1=1¶m2=2,那么提取“param1=1¶m2=2”的最简单方法是什么?
回答by Joseph
Use $_SERVER['QUERY_STRING']to access everything after the question mark.
使用$_SERVER['QUERY_STRING']访问所有的问号。
So if you have the url:
所以如果你有网址:
http://www.sample.com/page.php?param1=1¶m2=2
then this:
那么这个:
$url = "http://www.sample.com/page2.php?".$_SERVER['QUERY_STRING'];
echo $url;
will return:
将返回:
http://www.sample.com/page2.php?param1=1¶m2=2
回答by ivoba
In addition to Robs answer:
除了 Robs 的回答:
You can use http_build_queryand $_GET.
This is build-in and can deal with arrays.
Also you can easily manipulate the GET params this way, befor you put them together again.
您可以使用http_build_query和 $_GET。
这是内置的,可以处理数组。
您也可以通过这种方式轻松操作 GET 参数,然后再将它们组合在一起。
unset($_GET['unsetthis']);
$query = http_build_query($_GET);
回答by Rob
i would do
我会做
$querystring = '?'
foreach($_GET as $k=>$v) {
$querystring .= $k.'='.$v.'&';
}
$url .= substr($querystring, 0, -1);
where $urlalready contains everything before the ?
where$url已经包含 ? 之前的所有内容?
you could also use $_SERVER['QUERY_STRING']but as per the PHP manual:
您也可以使用$_SERVER['QUERY_STRING']但根据 PHP 手册:
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. *There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. *
$_SERVER 是一个包含标题、路径和脚本位置等信息的数组。此数组中的条目由 Web 服务器创建。*不保证每个网络服务器都会提供任何这些;服务器可能会省略一些,或提供此处未列出的其他内容。*

