I'm using a session for setting language
if(!isset($GLOBALS['lang'])){
$GLOBALS['lang'] = 'en';
}
Then I'm using ajax to update this:
var lang = 'no';
$.ajax({
type: "POST",
url: url,
data: {
lang : lang
},
success: function (data) { (...) }
});
The file being called looks like this:
global $lang;
if(strlen($_POST['lang']) == 2 ){
$lang = $_POST['lang'];
$result = array('lang_set' => $lang);
echo json_encode($result);
}
But my global session is not changed. I'm guessing this is due to the fact that lang.php uses another session instance.
I'm using Wordpress so I'm looking into if I can use some of the built in functions for this purpose. But I'm wondering if I can use PHP sessions for keeping track of selected language? Or do I have to use another method like adding selected language to my url?
UPDATE
Thanks to Ghost, I made it work. If you are using Wordpress, I'm doing the following in functions.php:
// Initialize session
if(session_id() == '') {
session_start();
}
// Set lang session with default language
if(!isset($_SESSION['lang'])){
$_SESSION['lang'] = 'no';
}
//globals
$GLOBALS['lang'] = $_SESSION['lang'];
if( !session_id() ){session_start();}