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 23Warning: require(lang/.php): failed to open stream: No such file or
directory in /home/claudio/public_html/index.php on line 23Fatal 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?
if
$_GET["lang"]and$_SESSION["lang"]are not set and$_COOKIE["lang"]is set, the only thing executed would be:If this is not the case, maybe you should check if the session save path in /var/php_sessions is writable by the web server.