• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Update of the variable $_SESSION following the link


I would like to update the variable $_SESSION when I click on one of the links (click on Spanish -> $_SESSION["lang"] = "es")

Index.html :
    if (!isset($_SESSION['lang'])) { $_SESSION['lang']='en';  } // -- default language
<!DOCTYPE html>
<html lang="en">

    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <h1>The language is <?php echo $_SESSION['lang'] ?></h1>
        <a href="changeLanguage.php?lang=en"><img src="en.png">English</a>
        <a href="changeLanguage.php?lang=es"><img src="es.png">Spanish</a>
        <a href="changeLanguage.php?lang=fr"><img src="fr.png">French</a>
        <a href="changeLanguage.php?lang=zh"><img src="zh.png">Chinese</a>
        <a href="changeLanguage.php?lang=ja"><img src="ja.png">Japanese</a>

changeLanguage.php :
$lang = $_GET['lang'] ?? null;
if (!in_array($lang, ['en', 'es', 'fr', 'zh', 'ja'])) { exit($_SESSION['lang'] = 'en'); }

$_SESSION['lang'] = $lang;

header('Location: index.php'); // Redirection

Thanks for your help