KotuS
KotuS

Reputation: 41

PHP4 including file during session

I am trying to put second language on my webpage. I decided to use different files for different languages told apart by path - language/pl/projects.ln contains Polish text, language/en/projects.ln - English. Those extensions are just to tell language files from other, the content is simple php:

$lang["desc"]["fabrics"]["title"] = "MATERIAŁY";
$lang["desc"]["fabrics"]["short_text"] = "Jakiś tam tekst na temat materiałów";
$lang["desc"]["services"]["title"] = "USŁUGI";
$lang["desc"]["services"]["short_text"] = "Jakiś tam tekst na temat usłóg";

And then on the index page I use it like so:

session_start();  
if (isset($_SESSION["lang"])) {  
    $language = $_SESSION["lang"];  
} else {  
    $language = "pl";  
}  
include_once("language/$language/projects.ln");
print $lang["desc"]["fabrics"]["title"];

The problem is that if the session variable is not set everything works fine and array item content is displayed but once I change and set $_SESSION["lang"] nothing is displayed. I tested if the include itself works as it should by putting print "sth"; at the beginning of projects.ln file and that works all right both with $_SESSION["lang"] set and unset.

Please help.

Upvotes: 1

Views: 80

Answers (2)

KotuS
KotuS

Reputation: 41

Session starts fine and accidentally I managed to fix it.

I renamed $_SESSION['lang'] to $_SESSION['curr_lang'] and it now works allright. It seams like it didn't like the array and session variable having the same name (?).

Upvotes: 0

eyescream
eyescream

Reputation: 19612

  1. Can you test the return value of session_start() - if it's false, it failed to start the session.
  2. Is it being called before you output anything to the browser? If headers were already sent and your error_reporting level is too low, you won't even see the error message.
  3. Stupid, but - do you set value of $_SESSION['lang'] to valid value like "en"? Does the English translation load correctly when you use it as default value in else block instead of "pl"?
  4. "Jakiś tam tekst na temat usłóg" -> "usług" :)

Can you tell us what does this one output:

if(session_start()) {
    echo SID, '<br/>';
    if(isset($_SESSION['lang'])) {
        echo 'lang = "',$_SESSION['lang'], '"';
    }
}

Upvotes: 1

Related Questions