-1

[RESOLVED]
In my login.php, several $_SESSION variables are set.
I've recently added in another = $_SESSION['darkM'] = false;.
Doing a var_dump($_SESSION); (Results below), my other variable's values are set perfectly but this one just will not!

I've tried setting it to a string instead ('test') but still returns empty in the var_dump. It is only this variable that will not set.
I've checked my error_log and there is nothing! Just to clear up, session_start(); is already set just above where I declare my variables. Any idea why this could be happening?


----- EDIT -----

Login.php:

<?php
session_start();

   // Store data in session variables
   $_SESSION["loggedin"] = true; // No error
   $_SESSION["tenant"] = $tenant; // No error
   $_SESSION['darkM'] = false; // Also tried setting to 'test'

VAR_DUMP($_SESSION) :

Array ( [loggedin] => 1 [tenant] => Coledon  [darkM] => )

RESOLVED

I have no idea why this made any difference, but I changed the variable from $_SESSION['darkM'] to $_SESSION['dark_mode']. Doing another var_dump the new result is: array(1) { ['dark_mode']=> bool(false) }

So I no longer have an issue, but still have no idea why this happened? There was no typing issues/hidden characters.
Also if anyone has this same problem please see navnath's & Reflective's answers - important to remember!

  • You should share your code instead of just description – navnath Oct 09 '21 at 17:59
  • 1
    So by this "`session_start();` is already set just above where I declare my variables", you're saying you've called `session_start()` before setting this `$_SESSION['darkM'] = false;.` right? If not, that would cause the scenario that you are describing. – Salvino D'sa Oct 09 '21 at 18:03
  • @Salvino -- That is correct yes. I also thought that was my mistake, but its placed correctly :( –  Oct 09 '21 at 18:11
  • @navnath -- I will edit my question now and share code –  Oct 09 '21 at 18:11
  • 2
    @Coledon_Projects Use `var_dump()` instead of `print_r()` for debugging purposes (the output you get is from `print_r()`). Also [edit] your question to include a [mcve] of the problem you have. – Progman Oct 09 '21 at 18:25
  • 1
    Did you try to re enter the full line in case there is some invisible character that prevent code execution. Did you also try to put it in first position ? – Ptit Xav Oct 09 '21 at 18:26
  • Just to make sure you actually tried what you're showing: create a new file `test.php`, _manually type this code over_, and run it (over localhost, of course, not from file). Because I suspect you didn't, and you'll see something different happening compared to what your post claims happens for this code. – Mike 'Pomax' Kamermans Oct 09 '21 at 18:28
  • @Mike'Pomax'Kamermans - Yes recreating this in Localhost worked. But I need this to work on the actual server. I deleted my code and completely retyped and got the same issue. –  Oct 09 '21 at 18:42
  • @PtitXav -- I deleted my code and completely retyped and got the same issue. I really have no idea why this error is happening. –  Oct 09 '21 at 18:44
  • Everyone I've updated my question - changing the variable name resolved the problem. BUT when I tried the old name again, the problem came back... –  Oct 09 '21 at 19:05
  • @Coledon_Projects running on localhost means "running it locally with a full server setup so that it behaves the same as your stage/prod instance". If your localhost setup doesn't mirror your prod instance, that will certainly be the first thing to solve before debugging any future problems. – Mike 'Pomax' Kamermans Oct 09 '21 at 19:31

2 Answers2

2

No problem at all, just print_r treats false differently than you may expect. Use var_dump as it shows the value and type of the variables instead of converting them to string which print_r does.

<?php
    session_start();
    $_SESSION['darkMode'] = false;
    print_r($_SESSION);
    var_dump($_SESSION);
?>

Output

Array
(
    [darkMode] => 
)

array(1) {
  ["darkMode"]=>
  bool(false)
}
Reflective
  • 3,854
  • 1
  • 13
  • 25
  • 1
    Thanks for the answer Reflective, that is correct. I also tried changing the variable's value to a string and it threw the same error. –  Oct 09 '21 at 18:48
  • I don't this answer helps in anyway, sorry @Reflective. Reason being that @Coledon_Projects has already mentioned in the question that he tried setting a string value `test` for that same variable and it was not being set. Ignoring the fact that he also mentioned the use of `var_dump` instead of `print_r` in code. I'm not downvoting this, but I really think it's just verbose and unnecessary. – Salvino D'sa Oct 09 '21 at 18:58
  • Everyone I've updated my question - changing the variable name resolved the problem. BUT when I tried the old name again, the problem came back... –  Oct 09 '21 at 19:09
  • @Reflective yeah I understand. I'm just saying it shouldn't have been an answer. But more of a comment against the question. As you see, it's still not answering why the variable was not showing rather it's just diverting the poster to a coding practice. – Salvino D'sa Oct 09 '21 at 19:14
0

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

Doc

navnath
  • 3,548
  • 1
  • 11
  • 19
  • Thanks for the answer navnath, that is correct. I also tried changing the variable's value to a string and it threw the same error. –  Oct 09 '21 at 18:47
  • What is the error now? Still you are not getting value for it? – navnath Oct 09 '21 at 18:51
  • Just checked again, the result is: `array(1) { ["darkM"]=> }` -- I have no idea why this is happening –  Oct 09 '21 at 18:56
  • 1
    `error_reporting(E_ALL);` to report all errors – navnath Oct 09 '21 at 19:02
  • Everyone I've updated my question - changing the variable name resolved the problem. BUT when I tried the old name again, the problem came back... –  Oct 09 '21 at 19:09