How to get and change URL variable PHP(如何获取和更改 URL 变量 PHP)
问题描述
谁能帮我解决这个问题我有一个像
Hi could anyone help me with this I have a URL like
parent/child/a=1&b=2$c=3
然后我有一个链接可以向该 URL 添加变量
then I have a link that would add variable to that URL
<a href="<?php echo $_SERVER["REQUEST_URI"]."&d=test1";?>">LINK 1</a>
<a href="<?php echo $_SERVER["REQUEST_URI"]."&d=test2";?>">LINK 2</a>
每次我点击链接时,指向 URL 的变量 d 都会像这样继续复制
every time I click my link the variable d to URL keep on reproducing like this
parent/child/a=1&b=2&c=3&d=test2&d=test2&d=test2&d=test1&d=test1
我知道 $_SERVER["REQUEST_URI"] 不断获取当前 URL,这就是我得到该结果的原因.我已经尝试了 $_SERVER[""] 的一些属性.我喜欢的是改变 d 变量值,不知道怎么做.非常感谢任何回复.谢谢!
I know that the $_SERVER["REQUEST_URI"] keep getting the current URL that is why I get that result. I have tried the some of properties of $_SERVER[""]. What I like is to change the d variable value, any idea how to do it. Any response is well appreciated.Thanks!
推荐答案
$query = $_GET;
// replace parameter(s)
$query['d'] = 'new_value';
// rebuild url
$query_result = http_build_query($query);
// new link
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $query_result; ?>">Link</a>
这篇关于如何获取和更改 URL 变量 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获取和更改 URL 变量 PHP
基础教程推荐
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01