简体   繁体   中英

How to remove or hide ?lang= from url if select other languages

I have a language selector script. The codes looks like these:

index.php

<?php

session_start();

$allowed_lang = array('en','de');

if (isset($_GET['lang']) === true && in_array($_GET['lang'], $allowed_lang) === true) {
    $_SESSION['lang'] = $_GET['lang'];
} else if (isset($_SESSION['lang']) === false) {
    $_SESSION['lang'] = 'en';
}

include ''. $_SESSION['lang'] .'.php';

echo $lang['hello'] , '!';

?>
<ul>
    <li><a href="?lang=en">English</a></li>
    <li><a href="?lang=de">Deutsch</a></li>
</ul>

en.php

<?php

$lang = array(
    'hello' => 'Hello',
);

?>

de.php

<?php

$lang = array(
    'hello' => 'Hallo',
);

?>

The default language is english of course, and if I select another language (eg German), then after the language change the url is index.php?lang=de . After switching to English the url is index.php?lang=en .

How can I remove or hide ?lang=en or ?lang=de from the url, so if I click the "deutsch" link, tthe language will switch but not append anything to the current URL (eg index.php )?

Thank you in advance for your answers!

Instead of using GET parameters for the language selection, you could use POST parameters. So you'd have to make the language selector a form which calls the document itself (ie without an action attribute, but with method="post" ) and send and receive the language selection as a POST variable, ie not visible.

After that (ie for the following pages the user switches to) you could use session variables, cookies or local storage to save and call the selected language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM