gpt4 book ai didi

php - 如何将 'add' 和 'subtract' $变量转换为 URL 字符串?

转载 作者:行者123 更新时间:2023-12-01 05:40:06 24 4
gpt4 key购买 nike

这里有问题....

我似乎找不到将 $variables“添加”和“减去”到 URL 字符串的好方法....

这是我的例子:

在 url 我有 http://mywebsite.com/index.php?y=2011

对于源代码我有:

$currentPage = $_SERVER['PHP_SELF'];

然后如果我想在 url 字符串中添加不同的 $variables,我一直在尝试这个...:

    echo '<a href="$currentPage.&t=col">College</a>';

但它没有保留当前 URL 的值.. .?

它显示:http://mywebsite.com/index.php?&t=col

而不是:http://mywebsite.com/index.php?y=2011&t=col (不保留 $y 变量)

我还需要找到一种通过任何链接更改 $y 和 $t 变量的方法,这样我就可以轻松地将其更改为 2010 年或 2009 年,同时保持当前的 $t 变量。???

提前致谢!

这是我在大神的帮助下得出的结论!!!

$url = "http://www.mywebsite/index.php?";
foreach($_GET as $key=>$val){
$url.="$key=$val&"; }
echo '<a href="' . htmlspecialchars($url . 't=col') . '">College</a>';

最佳答案

您可能知道,通过 URL 传递的所有变量都在 $_GET 数组中可用。因此,您可以像这样设置您的链接:

$url = "http://site.com/index.php?";
foreach($_GET as $key=>$val){
$url.="$key=$val&"; }

这给您带来的好处是还可以检查某些变量并删除一些可能由用户恶意添加的变量。它还允许您相应地更改变量,因为 $key 将包含 t 或 y

关于php - 如何将 'add' 和 'subtract' $变量转换为 URL 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6848928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com