0

我试图根据当前设置的 GET 参数创建一些链接。

我的网址如下所示:

http://mysite.com/index.php?bar=test&page=page

在我的代码中,我执行以下操作:

$bar = $_REQUEST['bar'];
<a href="index.php?bar=<?php echo $bar?>&page=anotherpage"

但是每次我单击链接时,它都会再次将整个字符串添加到 URL 中。

就像第一次点击会给我这个网址:

http://mysite.com/index.php?bar=test&page=anotherpagepage=anotherpage

下一步点击创建:

http://mysite.com/index.php?bar=test&page=anotherpagepage=anotherpagepage=anotherpage

等等。

有没有办法只获取一次请求,以便 URL 始终如下所示:

http://mysite.com/index.php?bar=test&page=anotherpage

无论我点击链接多少次?

非常感谢!

4

2 回答 2

1

您在第一个示例中错过了一个&符号。(&)。试试这个:

$bar = $_REQUEST['bar'];
<a href="index.php?bar=<?php echo $bar?>&amp;page=anotherpage"

或者更好的是,在使用前转义变量以防止 XSS、跨站点脚本安全漏洞。用于urlencode()URL。
http://nl.php.net/manual/en/function.urlencode.php

$bar = $_REQUEST['bar'];
<a href="index.php?bar=<?=urlencode($bar)?>&amp;page=anotherpage"
于 2012-09-25T21:22:55.473 回答
0

你应该看看 php 函数http_build_query

这使您可以先构建数组,如下所示:

$query = array("bar"=>$_REQUEST['bar'], "page"=>"anotherpage")
echo '<a href="/index.php?'.http_build_query($query).'">Link</a>';
于 2012-09-25T21:25:12.823 回答