0

I have a problem with my PHP (version 5.6) session. They are not being kept between pages, or even when I refresh my page.

On top of all my files I have :

<?php
session_start();

(there is no space or anything before).

When the user click on link, it's calling a function to set the website language to English or French. After some verification on the $_GET["lang"] value, I create the session like that :

$_SESSION["lang"] = $_GET["lang"];

If I do

var_dump($_SESSION["lang"]);

the server return string(2) "fr" or string(2) "en" regarding to which language the user select. Until there, everything is working great ;)

Problem is if I refresh the page, or go to another one, and try to return the session value, it's always NULL...

I know i could user other ways to translate the website, but I need the sessions to work for other functionality of my application (login, ...)

Because it was working few days ago, I first supposed it was a changed on the server, so I've contact my server administrator, but they told me they didn't change anything. I have the PHP error reporting set to E_ALL but no errors are displayed...

If someone could help me with that, it would be great, i'm stuck on this bug since 3 days now ^^.

Thanks !

EDIT :

session_start();
var_dump($_SESSION["lang"]);
if(!isset($_SESSION["lang"]) || $_SESSION["lang"] == null){
    $_SESSION["lang"] = "fr";
}
if(isset($_GET["lang"]) && ($_GET["lang"] == "fr" || $_GET["lang"] == "en")){
    $_SESSION["lang"] = $_GET["lang"];
}
var_dump($_SESSION["lang"]);
22
  • 1
    Put echo session_id(); after session_start(). If you try to refresh the page, this value changes? Commented May 24, 2015 at 15:11
  • Check if you change or unset the SESSION in the top of your page or somewhere else Commented May 24, 2015 at 15:13
  • Yes it does change on every refresh @SulthanAllaudeen I already check that but no i don't, I only unset my session on the logout page, which is never called on this part of the website Commented May 24, 2015 at 15:14
  • See if you write session_start in one php file then either include it in other php files(that is not good). Or in every php file write session_start at top. One more problem may occurring which is pointed by @Leggendario Commented May 24, 2015 at 15:14
  • Then your problem is that a new and empty session start every time. Commented May 24, 2015 at 15:17

2 Answers 2

2

from the limited code you're providing, my guess is that the $_GET argument isn't always set, which would then set the session to null.

try this...

if(isset($_GET["lang"])) {
    $_SESSION["lang"] = $_GET["lang"];
} else {
    echo 'lang not set';
}

EDIT: OP provided additional code.

This will return 'fr' if no value, or if an acceptable value hasn't been provided in the URL arguments. it's similar to what you have, however, I've wrapped the argument check in parentheses to make it a little tighter and changed the order. Your code was returning 'en' if nothing was provided.

session_start();

if(isset($_GET['lang']) && ($_GET['lang'] == 'fr' || $_GET['lang'] == 'en')) {
    if($_GET['lang'] == 'fr') {
        $_SESSION['lang'] = 'fr';
    }
    elseif($_GET['lang'] == 'en') {
        $_SESSION['lang'] = 'en';
    }
} else {
    $_SESSION['lang'] = 'fr';
}

var_dump($_SESSION['lang']);
Sign up to request clarification or add additional context in comments.

Comments

1

I found it ! My index.php file was encoded in UTF-8, i changed it to UTF-8 without BOM and it worked ! Really weird bug, I hope it will help someone ;)

2 Comments

next time enables the display of the errors! if you did that you would have noticed that session_start throw a warning
My error are displayed. It didn't return any warning, the session_start() was initiating each time that's why my session_id was never the same

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.