php - if $_GET == $lang multi-lingual page changes -


this select form loads right domain, page , language variable when clicked. when user changes language 2 times in row url has 2 language variables. example: domain.com/page.php?lang=en_us?lang=nl_be

how prevent this?

<div class="country_selector"><?php echo $lang['region']; ?>         <select id="source" onchange="navigateto(this, 'window', false);">             <option value="https://domain.cc<?php echo $_server['request_uri']; ?>?lang=en_eu">europa</option>             <option value="https://domain.be<?php echo $_server['request_uri']; ?>?lang=nl_be">belgiƫ</option>             <option  value="https://domain.nl<?php echo $_server['request_uri']; ?>?lang=nl_nl">nederland</option>             <option  value="https://domain.uk<?php echo $_server['request_uri']; ?>?lang=en_uk">united kingdom</option>             <option  value="https://domain.cc<?php echo $_server['request_uri']; ?>?lang=en_us">united states</option>         </select>     </div> 

$_server['request_uri'] includes query string. use $_server['php_self'] instead.


another option, if have more parameters in $_server['request_uri'] want preserve in generated urls write function analyzes $_get[] or $_server['query_string'], adds or replaces lang parameter , generates url:

function generatelanguageurl(array $params, $lang) {     // add language $params (or replace if it's there)     $params['lang'] = $lang;      // generate query string, append script's uri , return     // generate partial urls. there no need put protocol     // , host name in url if same of current page.     return $_server['php_self'].'?'.http_build_query($params); } 

the first argument (array $params) lets use $_get generate urls contain parameters of current query string, array() generate urls without other parameters or other combination of parameters need (e.g. array('q' => 'foo', 'items' => 10, 'page' => 2)).

how use it:

<div class="country_selector"><?php echo $lang['region']; ?>     <select id="source" onchange="navigateto(this, 'window', false);">         <option value="<?php echo generatelanguageurl($_get, 'en_eu'); ?>">europa</option>         <option value="<?php echo generatelanguageurl($_get, 'nl_be'); ?>">belgiƫ</option>         <option value="<?php echo generatelanguageurl($_get, 'nl_nl'); ?>">nederland</option>         <option value="<?php echo generatelanguageurl($_get, 'en_uk'); ?>">united kingdom</option>         <option value="<?php echo generatelanguageurl($_get, 'en_us'); ?>">united states</option>     </select> </div> 

you can improve function let generate url need site adding script path, hostname , protocol optional arguments, using values of current page defaults.


Comments