dongyihao1099 2014-06-23 13:31
浏览 38
已采纳

URL中取消设置变量会产生错误

I've recently been developing a multi-lingual website. I've got a slight problem though. Every time a button is clicked the language variable needs to change. i did this using the anchor tag (i.e <a href="?lang=En">English</a> ).

The problem arises when other variables besides the language are added to the URL. I would like to redirect the page without getting rid of other variables and just changing the lang variable. So if the url contains "var1=value&var2=value&lang=En", I would like to alter the lang variable and keep the rest as they are. The lang variable can have 3 values: En, Az, Ru.

The method I tried so far:

function URI_ADD_AZ(){
    $URI = $_SERVER['REQUEST_URI'];

    if(isset($_GET['lang'])){
        $lang = $_GET['lang'];
        unset($lang);
    }
    $new_URI = $URI . '?lang=Az';
    return $new_URI;
}

<a href="<?php URI_ADD_AZ?>">Azeri</a>

The problem:

Everytime the button is clicked the lang variable just gets added to the url not altered:

/?lang=Az?lang=Az?lang=Az?lang=Az

How can I make sure it does not keep getting repeated and avoid redirect loops?

  • 写回答

2条回答 默认 最新

  • dongpaocuan7498 2014-06-23 13:43
    关注

    The URI_ADD_AZ function posted in the question does not overwrite or remove preexisting occurrences of lang=* in the Query String, therefore duplication of "langs" in the URL. Also there is no handling of the requirement for ? or & depending on location of the key=value pair in the query string.

    Here, to simplify things and limit the working string and therefore potential for introducing errors, the $_SERVER var QUERY_STRING is pulled, rather than the entire REQUEST_URI, and PHP_SELF is then prepended to the HREF value.

    First thing here is to remove the lang=* including the & depending on it's position. A conditional reference is used to remove the trailing & only if the match is found at the beginning of the string.

    Next $lang is retrieved from the $_GET var and validated. And if there's a valid $lang it is appended to the query string taking into consideration whether & is needed or not.

    Finally, if not empty, the resulting query string is prepended with ? and returned.

    function URI_ADD_AZ() {
        $QS = preg_replace('/((^)|&)lang=[^&]*(?(2)&)?/', '', $_SERVER['QUERY_STRING']);
        $lang = ( isset($_GET['lang']) && in_array($_GET['lang'], array('En','Az','Ru')) )? 'lang=' . $_GET['lang']: '';
        if ( '' != $lang ) {
          if ( '' == $QS ) { $QS = $lang; }
          else { $QS .= "&$lang"; }
        }
        if ( '' != $QS ) {
            return '?' . $QS;
        }
    }
    <a href="<?php echo $_SERVER['PHP_SELF'] . URI_ADD_AZ(); ?>">Azeri</a>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器