I have the following code:
<?php
ini_set('display_errors', 'On');error_reporting(E_ALL | E_STRICT);
session_start();
set_include_path('../include');
if(isset($_GET["lang"])
&& $_GET["lang"] != $_SESSION["lang"]
&& ($_GET["lang"] == 'en' || $_GET["lang"] =='pt')){
$_SESSION["lang"]= $_GET["lang"];
setcookie("lang", $_GET["lang"]);
}
if(!isset($_SESSION["lang"])){
if(isset($_COOKIE["lang"])){
echo $_SESSION["lang"] == $_COOKIE["lang"];
} else {
switch(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)){
case 'pt': $_SESSION["lang"] = "pt";setcookie("lang", $_SESSION["lang"]); break;
default: $_SESSION["lang"] = "en";setcookie("lang", $_SESSION["lang"]);
}
}
}
print_r($_SESSION);print_r($_GET);print_r($_COOKIE);
require('lang/'.$_SESSION["lang"].'.php');
?>
But for some reason the $_SESSION variables do not retain values. The output is the following:
Notice: Undefined index: lang in /home/claudio/public_html/index.php on line 13 Array ( ) Array ( [get] => get ) Array ( [lang] => en [PHPSESSID] => c92d58e58508gvjf2urfmr9uh3 ) Notice: Undefined index: lang in /home/claudio/public_html/index.php on line 23
Warning: require(lang/.php): failed to open stream: No such file or directory in /home/claudio/public_html/index.php on line 23
Fatal error: require(): Failed opening required 'lang/.php' (include_path='../include') in /home/claudio/public_html/index.php on line 23
If i do echo session_start(); it returns 1, so what can be the problem with the session?
echo $_SESSION["lang"] = $_COOKIE["lang"];. Your are comparing the values and not assigning in your code.ifs and boolean messes.getLocale()method, which handle all the logic. Then I would just check if the session / req language isin_array( $yourLanguagesHere ), so the app would be scalable and not tied just to english and portuguese.