如何获取和更改URL变量PHP [英] How to get and change URL variable PHP

查看:94
本文介绍了如何获取和更改URL变量PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 父/子/ a = 1& amp ; b = 2 $ c = 3 

然后我有一个链接将变量添加到该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>

每次点击我的链接时,变量d到URL都会继续像这样重现。

 父母/子女/ a = 1& b = 2& c = 3& d = test2& d = test2& d = test2& d = test1& amp ; d = test1 

我知道$ _SERVER [REQUEST_URI]继续获取当前URL这就是我得到这个结果的原因。我已经尝试了$ _SERVER []的一些属性。
我喜欢的是改变d变量值,任何想法如何去做。
任何回应都非常感谢。谢谢!

解决方案

  $ query = $ _得到; 
//替换参数
$ query ['d'] ='new_value';
//重建url
$ query_result = http_build_query($ query);
//新链接
< a href =<?php echo $ _SERVER ['PHP_SELF'];?>?<?php echo $ query_result;?>> Link< ; / A>


Hi could anyone help me with this I have a URL like

parent/child/a=1&b=2$c=3

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>

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

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆