简体   繁体   English

PHP如何在链接中保留所有GET变量?

[英]PHP How do I retain all GET vars in links?

I have some PHP code that generates dynamic tables of data on the fly. 我有一些PHP代码可以动态生成动态数据表。 By clicking various links you "refine" the tables of data. 通过单击各种链接,您可以“优化”数据表。 What I'd like is each of these links to retain the current GET information and add to it. 我想要的是这些链接中的每一个都保留当前的GET信息并添加到其中。 IE: IE:

$allPreviousVars = ???; // Could be 20+ vars
echo "<a href='".$allPreviousVars."&newVar=2'>Link</a>";

I can think of ways to do it by iterating through $_GET with a loop, but surely there is a quicker way to do this? 我可以通过循环迭代$ _GET来想办法,但肯定有更快的方法吗?

How about $_SERVER["QUERY_STRING"]? $ _SERVER [“QUERY_STRING”]怎么样?

EDIT: Since you were kind enough to give me credit for this answer, I should add one thing. 编辑:既然你很友好地给我这个答案,我应该补充一点。 You should wrap the above variable in htmlspecialchars() before you output it. 在输出之前,您应该将上面的变量包装在htmlspecialchars()中。 Otherwise someone could type a URL with "> in it, and it would break your link. 否则,有人可以在其中键入带有">的网址,这会破坏您的链接。

I do this as follows: 我这样做如下:

<?php echo http_build_query(array_merge($_GET, array('foo'=>'bar', 'foo2'=>'bar2')); ?>

Note that any existing 'foo' or 'foo2' keys would be replaced. 请注意,任何现有的'foo'或'foo2'键都将被替换。

Use http_build_query() if you need to generate a query-string from a modified array. 如果需要从修改后的数组生成查询字符串,请使用http_build_query() If you just want the querystring sent to the current page, do as suggested and use $_SERVER["QUERY_STRING"]. 如果您只想将查询字符串发送到当前页面,请按照建议操作并使用$ _SERVER [“QUERY_STRING”]。

I would probably do this: 我可能会这样做:

$query = mySanitizeFunction($_GET);
$url = http_build_query($query) . '&newVar=2';

你最好的选择是,如你所建议的那样,循环遍历$_GET的内容,从现有查询参数和被覆盖的位的混合构造URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM