dongshushen4392 2011-07-27 09:54
浏览 81
已采纳

如何'添加'和'减去'$变量到URL字符串?

Having a problem here....

I can't seem to find a good way to 'add' and 'subtract' $variables to an URL string....

Here is my example:

in the url I have http://mywebsite.com/index.php?y=2011

For source code I have:

$currentPage = $_SERVER['PHP_SELF'];

Then if I want to add different $variables to the url string, I have been trying this...:

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

But it isn't keeping the value of the current URL.. .?

It displays : http://mywebsite.com/index.php?&t=col

Instead of : http://mywebsite.com/index.php?y=2011&t=col (doesn't keep the $y variable)

I also need to find a way to change the $y and $t variable by any links, so I can easily change it to 2010, or 2009, with keeping the current $t variable.. ???

Thanks in advance!

HERE IS What I came up with from the great help!!!

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

4条回答 默认 最新

  • douque8855 2011-07-27 09:59
    关注

    As you may know, all variables passed through an URL are available in the $_GET array. Therefore, you can set up your link with something like this:

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

    This gives you the benefit of also checking for certain variables and removing some that might be added by the user maliciously. And it also allows you to change your variables accordingly, as $key will contain either t or y

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部