[英]Pass current GET Variables with href
Let's say I am on www.example.com/?par1=test
and I have a Link like: <a href="/subpage">Link text</a>
. 假设我在
www.example.com/?par1=test
并且有一个类似<a href="/subpage">Link text</a>
。
How to pass the GET Variable to the subpage, without reading every Parameter eG $_GET["par1"]
and pass it to the Link by hand. 如何在不读取每个参数eG
$_GET["par1"]
并将其手动传递给Link的情况下,将GET变量传递给子页面。
$_SERVER['QUERY_STRING'] contains the data that you are looking for. $ _SERVER ['QUERY_STRING']包含您要查找的数据。
If you want to pass all get variables to next page just do next thing: 如果您要将所有的获取变量传递给下一页,只需执行下一项操作:
$href = '/somepage/';
if ($_GET) {
$href .= strpos($href, '?') === false ? '?' : '&';
$href .= http_build_query($_GET);
}
Then echo this href 然后回显此href
<a href="<?=$href;?>">my link</a>
<a href='www.example.com.br?pas1=<?php echo $variavel ?>'>Link</a>;
You could create a function to aid your link generation. 您可以创建一个函数来帮助您生成链接。
Obviously this is a simple example but you could easily extend it. 显然,这是一个简单的示例,但是您可以轻松地对其进行扩展。
function getLink($url, array $params = array())
{
if (empty($params)) return $url;
$url .= '?';
foreach($params as $param => $value) {
$url .= $param .'='. $value;
}
return $url;
}
$params = array(
'test' => 1,
'foo' => 'hello',
'bar' => 'test',
);
echo getLink('my/target/page.php', $params);
The $params
array could be substituted for the super-global $_GET
to make life easier! $params
数组可以代替超全局$_GET
使生活更轻松!
As with many problems, there are many solutions! 与许多问题一样,有许多解决方案!
The simplest is probably just to append your URL with the contents of $_SERVER['QUERY_STRING']
, eg: 最简单的方法可能只是在URL
$_SERVER['QUERY_STRING']
附加$_SERVER['QUERY_STRING']
,例如:
<a href="/somefile?<?php echo $_SERVER['QUERY_STRING'] ?>">somefile</a>
But this does rely on your webserver correctly passing through the correct parameters I believe. 但这确实依赖于您的Web服务器正确传递我相信的正确参数。 Apache should do this out of the box with mod_php, but FastCGI based PHP may need some additional config.
Apache应该使用mod_php开箱即用,但是基于FastCGI的PHP可能需要一些其他配置。
Take a look at the documentation for the $_SERVER
variable 查看
$_SERVER
变量的文档
this is a example of how to do this : 这是如何执行此操作的示例:
<?php
echo print_r($_GET);
echo "<br>";
echo "www.mysite.com/index.php?".http_build_query($_GET);
?>
will print : (if url is : http://www.mysite.com/index.php?this=1&that=2 ) 将显示:(如果url为: http ://www.mysite.com/index.php?this=1& that=2 )
Array ( [this] => 1 [that] => 2 ) 1 数组([this] => 1 [that] => 2)1
www.mysite.com/index.php?this=1&that=2 www.mysite.com/index.php?this=1&that=2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.