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"]);
Thibaud Lacan
  • 346
  • 1
  • 6
  • 13
  • 1
    Put `echo session_id();` after `session_start()`. If you try to refresh the page, this value changes? – Federkun May 24 '15 at 15:11
  • Check if you change or unset the `SESSION` in the top of your page or somewhere else – Sulthan Allaudeen May 24 '15 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 – Thibaud Lacan May 24 '15 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 – Alive to die - Anant May 24 '15 at 15:14
  • Then your problem is that a new and empty session start every time. – Federkun May 24 '15 at 15:17
  • I wonder, If session is started twice then it forgets the old sessions' value ? That is not the exact reason – Sulthan Allaudeen May 24 '15 at 15:21
  • possible duplicate of [PHP Session data not being saved](http://stackoverflow.com/questions/155920/php-session-data-not-being-saved) – Madan Sapkota May 24 '15 at 15:21
  • @ThibaudLacan : Can you `var_dump($_SESSION); ` in both pages ? – Sulthan Allaudeen May 24 '15 at 15:22
  • My application is setting the session to "fr" if the session["lang"] doesn't exist. So first time you hit the website, var_dump($_SESSION) return : array(1) { ["lang"]=> string(2) "fr" } Then if i go to the link to set language to English, it return : array(1) { ["lang"]=> string(2) "en" } If i go back to home page, or refresh the page, I get french back – Thibaud Lacan May 24 '15 at 15:26
  • I'm wondering the same thing @SulthanAllaudeen, but i'm checking everything and have only a session_start on top of everything (i include my files on a index.php regarding to the url the guy is trying to access) – Thibaud Lacan May 24 '15 at 15:29
  • Your browser accept cookies? `var_dump(session_get_cookie_params());` what do you get? – Federkun May 24 '15 at 15:30
  • you need to provide more code. – timgavin May 24 '15 at 15:31
  • @Leggendario i get : array(5) { ["lifetime"]=> int(0) ["path"]=> string(1) "/" ["domain"]=> string(0) "" ["secure"]=> bool(false) ["httponly"]=> bool(false) } – Thibaud Lacan May 24 '15 at 15:31
  • @ThibaudLacan Can you Close all your browser and try it in a incognito or private mode from your browser. Make sure you not run the same page in other tab :) – Sulthan Allaudeen May 24 '15 at 15:32
  • Yep i'm working in incognito right now, I have a friend trying on his laptop next to me and another one testing from another location ;) – Thibaud Lacan May 24 '15 at 15:35
  • @ThibaudLacan Are you connected to lan to do it ? And now you able to get it ? – Sulthan Allaudeen May 24 '15 at 15:38
  • Yes my website is online already, i'm not working on local – Thibaud Lacan May 24 '15 at 15:42
  • After incognito it didn't worked ? – Sulthan Allaudeen May 24 '15 at 15:50
  • No still not working. Us 3 are trying on different browser (chrome, firefox and IE) incognito mode and cookies enable .... I check my session_save_path as well and it's a writable path – Thibaud Lacan May 24 '15 at 15:53
  • "When the user click on link, it's calling a function to set the website language to English or French. " Have you posted the function. Let's see that. – timgavin May 24 '15 at 15:59
  • My mistake @timgavin it's not a function, i'm checking the $_GET["lang"] and assign it to the session when page load. You can see the code at the bottom of my post – Thibaud Lacan May 24 '15 at 16:04
  • Thanks guys for your help. I found why : i needed to encode my index.php file in UTF-8 without BOM, it was encoded in UTF-8 only. Can't really explain why but at least it's fixed ! – Thibaud Lacan May 24 '15 at 16:18

2 Answers2

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']);
timgavin
  • 4,972
  • 4
  • 36
  • 48
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 ;)

Thibaud Lacan
  • 346
  • 1
  • 6
  • 13
  • next time enables the display of the errors! if you did that you would have noticed that session_start throw a warning – Federkun May 24 '15 at 17:58
  • 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 – Thibaud Lacan May 24 '15 at 18:26