Jump to content

Session Problem


rfighter

Recommended Posts

Hi,

 

I'm trying to use sessions on my forum script to allow users to change the language. At the bottom of the forum index there are the links to change between English and French. Clicking these links redirects to the same page but with a variable in the URL:

 

The page starts off in French.

 

http://www.X.com/forumindex.php?l=en

 

This is the code contained within the forumindex.php

include_once ("config.php");
include_once("online.php");

if(isset($_GET['l'])){

$lang = $_GET['l'];

If ($lang == "fr") {

$_SESSION['lang']="fr";
echo "set as FR";
echo "<br>";
echo $_SESSION['lang'];

} else {

$_SESSION['lang']="en";
echo "set as EN";
echo "<br>";
echo $_SESSION['lang'];

}

}

 

When the page reloads, it outputs the session variable 'lang' as "EN", which is correct, however the page still loads in French. The language function is in config.php (included at the top of forumindex.php), and when I ask it to output what the session variable 'lang' is set to in config.php, it says it is still FR...however I have just been told it is set to EN.

 

I beleive the problem is something to do with session variables not being set until the whole page has been output, but if this is the problem then why is it telling me that the variable *has* been set as EN?

 

The code in config.php is:

 

function lang($id) {

	if (isset($_SESSION["lang"])) {
	echo $_SESSION["lang"];


		if ($_SESSION["lang"]="fr") {


			$chosen_language = "french";
			//echo "language_set_as_fr";


		} else {


			$chosen_language = "english";

		}

	} else {


		$chosen_language = "english";
		//echo "language_not_set";

	}

	$lang_query = MYSQL_QUERY("SELECT * from language WHERE id ='$id'");
	$lang = mysql_fetch_array($lang_query);


	If ($chosen_language == "english") {


		$phrase = $lang[english];

	} elseif ($chosen_language == "spanish") {

		$phrase = $lang[spanish];

	} elseif ($chosen_language == "french") {

		$phrase = $lang[french];

	}

	return $phrase;

}

 

If anyone could help me, I would be really grateful. Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/138411-session-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.