0

I've been dealing with a problem for a long time, I'll try to describe it. I have languages to choose from in a Droppbox (so far only German and English). By default, English is set, if I now switch to German everything goes perfectly, but as soon as I try to switch back to English, it is as if I had selected German again.

In langugae.php I select the language and pass it with the post method to language.inc.php where with the help of the selected language an array is loaded from a database in which the individual phrases are.

The function Defaulttext() set the $_SESSION["text"]; variable to the English version and it is called at the top of the website.

language.php

<?php
    require_once 'conf/db-config.inc.php';
    $result = $db_xttv_sqli->query("SELECT name FROM rm_languages");
    $text = array();
    $text = $_SESSION["text"];
?>

<div>
    <form action="includes/language.inc.php" method="post" id="languageid">
        <label for="language" id="language_input">
            <select id="language" class="dropdown-content" name="language" onchange="this.form.submit();">
                <?php
                while($rows = $result->fetch_assoc()) {
                    $language = $rows['name'];              
                    if($_SESSION["language"] == $language) {
                        echo "<option selected value='$language '>$language</option>";
                    }
                    else {
                        echo "<option value='$language '>$language</option>";
                    }
                }
                ?>
            </select>
        </label>
        <input type="hidden" id="SelectedLanguage" name="language" value="<?php echo htmlspecialchars($language);?>">
        <input type="hidden" name="path" value="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
    </form>
</div>

language.inc.php

<?php
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

if (isset($_POST["language"])){ 
    require_once '../conf/db-config.inc.php';
    $language = $_POST['language'];
    $path = $_POST['path'];
    ChangeLanguage($language, $path, $db_xttv_sqli);
}

function Defaulttext($db_xttv_sqli) {
    if(isset($_SESSION["text"])) {

    }

    else {
        //Default English = 1
        $text = array();
        $result = $db_xttv_sqli->query("SELECT rm_texts_idrm_texts, text FROM rm_languages_has_rm_texts WHERE rm_languages_idrm_languages = 1");

        while($rows = $result->fetch_assoc()) {
            $text[$rows['rm_texts_idrm_texts']] = $rows['text'];
        }
        $_SESSION["text"] = $text;
        $_SESSION["language"] = "English";
    }
}

function ChangeLanguage($language,$path,$db_xttv_sqli) {
    $text = array();
    $query = "SELECT idrm_languages FROM rm_languages WHERE name = '$language'";
    $result = mysqli_query($db_xttv_sqli, $query); 
    $LanguageID = mysqli_fetch_array($result); 

    $intLiD = (int)$LanguageID[0];

    $result = $db_xttv_sqli->query("SELECT rm_texts_idrm_texts, text FROM rm_languages_has_rm_texts WHERE rm_languages_idrm_languages = $intLiD");
    while($rows = $result->fetch_assoc()) {
        $text[$rows['rm_texts_idrm_texts']] = $rows['text'];
    }

     $_SESSION["text"] = $text;
     $_SESSION["language"] = $language;

    header('location:'.$path);
}
A J
  • 3,575
  • 13
  • 36
  • 49
  • Hi, did you try to set session language in your function ChangeLanguage? $_SESSION["language"] = $language; An did you try to echo $_SESSION["language"] and $language in language.php? – Monnomcjo Mar 06 '21 at 11:39
  • i have forgotten the two lines in my statement sry. yes have already issued both with echo and alert after switching no matter which language I set, I always get German back – Florian Poppinger Mar 06 '21 at 12:05
  • Is your session start correctly in if (session_status() == PHP_SESSION_NONE)? – Monnomcjo Mar 06 '21 at 12:23
  • What do you mean with "correctly"? I thought session_start() is the correctly way, the if is just there to be sure a session has started. – Florian Poppinger Mar 06 '21 at 19:17
  • The "if" test depends on the version of PHP https://stackoverflow.com/questions/6249707/check-if-php-session-has-already-started – Monnomcjo Mar 07 '21 at 04:58
  • my version PHP is 7.3.9 so it should work this way – Florian Poppinger Mar 08 '21 at 09:41

0 Answers0