dongshushen4392 2011-07-27 17: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 17: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条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错