duanen19871021 2010-07-02 01:24
浏览 41
已采纳

如何将GET变量添加到php中当前url的结尾?

I am trying to add a few different GET variables to the url.

I could easily do a header redirect to the current page url and then just add the $_GET['test'] in the url.

My problem is that I have some GET variables that are in the url already. What I want to do is:

  • Check if there are any GET variables in the url

    • If there is not, then redirect to the current url with the new GET['test'] variable at the end of the url.

    • If there is, but there is no GET['test'] variable in the url, then keep those other GET values in the url and add the GET['test'] variable to end of the full url string

    • If there is, AND there is a GET['test'] variable in the url, then keep those other GET values in the url and exchange the GET['test'] variable value with the new value.

How can I go about checking for all these conditions?

  • 写回答

6条回答 默认 最新

  • duanchen7036 2010-07-02 01:32
    关注

    The simple way to it is:

    $params           = array_merge( $_GET, array( 'test' => 'testvalue' ) );
    $new_query_string = http_build_query( $params );
    

    This doesn't guarantee that test will be at the end. If for some odd reason you need that, you can just do:

    $params = $_GET;
    unset( $params['test'] );
    $params['test']   = 'testvalue';
    $new_query_string = http_build_query( $params );
    

    Note, however, that PHP query string parameter parsing may have some interoperability problems with other applications. In particular, PHP doesn't accept multiple values for any parameter unless it has an array-like name.

    Then you can just forward to

    ( empty( $_SERVER['HTTPS'] ) ? 'http://' : 'https://' ) .
    ( empty( $_SERVER['HTTP_HOST'] ) ? $defaultHost : $_SERVER['HTTP_HOST'] ) .
    $_SERVER['REQUEST_URI'] . '?' . $new_query_string
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧