php 获取所有 url 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9624803/
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 get all url variables
提问by user962449
I'm trying to make links to include the current _GET variables.
我正在尝试创建包含当前 _GET 变量的链接。
Example link: <a href="?page=2">Page 2</a>
示例链接: <a href="?page=2">Page 2</a>
The current url is: http://example.com/test.php?id=2&a=1
当前网址为:http: //example.com/test.php?id=2&a=1
So if someone clicks on the link of page 2 it will take them to: http://example.com/test.php?id=2&a=1&page=2
因此,如果有人点击第 2 页的链接,则会将他们带到:http: //example.com/test.php?id=2&a=1&page=2
Currently if they click on the link it takes them to: http://example.com/test.php?page=2
目前,如果他们点击链接,他们将前往:http: //example.com/test.php?page=2
As you can see, I need a way to get the current _GET variables in the url and add them to the link. Advice?
如您所见,我需要一种方法来获取 url 中的当前 _GET 变量并将它们添加到链接中。建议?
回答by Your Common Sense
$new_query_string = http_build_query(array_merge($_GET,array('page' => 2)));
回答by octern
The superglobal entry $_SERVER['QUERY_STRING']
has the query string in it. You could just append that to any further links.
超全局条目中$_SERVER['QUERY_STRING']
包含查询字符串。您可以将其附加到任何进一步的链接。
update:The alternate response on this page using http_build_query is better because it lets you add new variables to the query string without worrying about extraneous ?s and such. But I'll leave this here because I wanted to mention that you can access the literal query string that's in the current address.
更新:此页面上使用 http_build_query 的替代响应更好,因为它可以让您向查询字符串添加新变量,而无需担心无关的 ?s 等。但我将把它留在这里是因为我想提到您可以访问当前地址中的文字查询字符串。
回答by Shankar Damodaran
Make use of @extract($_GET)
. So you can access them directly as variables.
利用@extract($_GET)
. 所以你可以直接作为变量访问它们。
回答by edCoder
Try this may help you......
试试这个可能对你有帮助......
function get_all_get()
{
$output = "?";
$firstRun = true;
foreach($_GET as $key=>$val) {
if($key != $parameter) {
if(!$firstRun) {
$output .= "&";
} else {
$firstRun = false;
}
$output .= $key."=".$val;
}
}
return $output;
}
回答by Szél Lajos
As for the question above how to include the name of php file as well in the url, using Your Common Sense's perfect method and adding a question mark worked for me:
至于上面的问题如何在url中包含php文件的名称,使用你的常识的完美方法并添加一个问号对我有用:
echo "<a href='?".$url."'>link</a>"