0

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?

4
  • 1
    Try echo $_SESSION["lang"] = $_COOKIE["lang"];. Your are comparing the values and not assigning in your code. Commented Nov 13, 2012 at 10:42
  • 2
    Btw, consider refactoring your logic because it is over complicated / too many nested ifs and boolean messes. Commented Nov 13, 2012 at 10:42
  • @moonwave99 Can you give an example of how can i check if lang cookie and session is set, and also verify if there are any prompt to change the lang in GET? It was the smallest thing i could do in 10 minutes :( Commented Nov 13, 2012 at 10:50
  • @Claudiop first of all, I would go with an object wrapping all the logic inside a getLocale() method, which handle all the logic. Then I would just check if the session / req language is in_array( $yourLanguagesHere ), so the app would be scalable and not tied just to english and portuguese. Commented Nov 13, 2012 at 11:34

1 Answer 1

1

if $_GET["lang"] and $_SESSION["lang"] are not set and $_COOKIE["lang"] is set, the only thing executed would be:

echo $_SESSION["lang"] == $_COOKIE["lang"]; //maybe this should be a '='?

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thats right. Sorry for being so distracted. I took some minutes looking for such errors, but unfortunately i found nothing...
Yes, that solved. It was my distraction to leave that "==". As the code already worked before (without cookie set), i discarded some verifications. Thanks a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.