Search code examples
phphtmlhyperlinkhref

PHP - link for actual language id


So I'm building a small multilingual (French + English) website and there is a little bug. I would like to remove the "english default" language in the code, so if a user picks french on one page, any page he will select afterwards will be in french and not be "back" to english. Same if the user picks english in the first place. But since there will be more french users, I would like it to be the default language on the home page.

The content is in folder with prefixes: fr_language.php and en_language.php

The lang.php file here

Links for FR or EN

    <a href="?lang=fr">Français</a>
    <a href="?lang=en">English</a>

And the navigation

<ul>
    <li><a href="index.php"><?php echo $lang['home']; ?></a></li>
    <li><a href="services.php"><?php echo $lang['services']; ?></a></li>
    <li><a href="about.php"><?php echo $lang['aboutus']; ?></a></li>
    <li><a href="contact.php"><?php echo $lang['contact']; ?></a></li>
</ul>

Thanks for your help!

EDIT:

Ok great, with your help it's working! In my "nav.php" include, I wrote this. Perhaps it's possible to do a cleaner version, haha! Any ideas? Thanks again!


Solution

  • Consider using the following approach/method:

    if(isset($_GET['lang']) && $_GET['lang'] == "fr"){ 
    
       // do something 
    
    } else { 
       // do something else 
    }
    

    You should also be made aware of (XSS) Cross-site scripting.

    Here are a few links to read up on:

    You can further your research by using "XSS injection php" as keywords in your favorite search engine.