$_SESSION variable is null before I set it to null

Multi tool use


$_SESSION variable is null before I set it to null
I have encountered a weird problem in my php
code for a website I'm building:
php
I use the superglobal $_SESSION['recent']
for creating alerts on my webpages. For example if a user tries to sign in, but gives a wrong password, the sign-in form will send its data to a file login.php
which checks if all the data is correct. If the data is correct, it will set $_SESSION['recent'] = 'login_succes'
(and a few other things so the user is signed in), else it will set $_SESSION['recent'] = 'login_fail'
. Next it will return to the homepage with header('location: http://www.example.com/')
.
$_SESSION['recent']
login.php
$_SESSION['recent'] = 'login_succes'
$_SESSION['recent'] = 'login_fail'
header('location: http://www.example.com/')
In this homepage, there is a chunk of php
code with a switch
statement which checks for all possible $_SESSION['recent']
values and will give an alert according to its value. This code looks a bit like this (I use bootstrap):
php
switch
$_SESSION['recent']
<?php switch ($_SESSION['recent']) { ?>
<?php case 'logout': ?>
<div class="container-fluid alert-fixed"><div class="alert alert-success alert-dismissible">
<button type="button" class="btn close" data-dismiss="alert">×</button>
You are signed out.
</div></div>
<?php break; case 'login_succes': ?>
<div class="container-fluid alert-fixed"><div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert">×</button>
You are signed in
</div></div>
<?php break; case 'login_fail': ?>
... (similar alert message)
<?php break; case 'not_loggedin': ?>
...
<?php break; case 'admin_change_succes': ?>
...
<?php break; case 'admin_change_fail': ?>
...
<?php break; } $_SESSION['recent'] = null; ?>
So what I expect it would do, is check for any notifications and show an alert message if necessary. When that is done, it would set $_SESSION['recent'] = null
so this message wont keep showing showing on and on.
$_SESSION['recent'] = null
However, this is the problem: this works for the first 3 cases (logout
, login_succes
and login_fail
), but for all the other cases, the alerts don't show, unless I delete the line $_SESSION['recent'] = null;
(which I obviously don't want to delete, because then the alert messages will keep on showing on reloading).
logout
login_succes
login_fail
$_SESSION['recent'] = null;
Does anyone know what is causing this and how to tackle the problem? If you need more information, just ask for it.
Thanks!
session_start();
unset($_SESSION['recent'])
Yes, it does have session_start() and using unset() doesn't work either...
– wohe1
3 hours ago
echo out
$_SESSION['recent']
just before the switch statement and make sure it has the values that you are looking for. Make sure the values match your case values.– Joseph_J
3 hours ago
$_SESSION['recent']
I already did, as I mentioned in my question: the alerts work perfectly when I remove the line containing
$_SESSION['recent'] = null;
– wohe1
3 hours ago
$_SESSION['recent'] = null;
@wohe1 I see no problem with your code and it should work as intended,but now it depends upon how the program control reaches this file.
– vivek_23
3 hours ago
2 Answers
2
The code you posted in question looks like it should work as you expect. Most likely the problem is somewhere outside. To make sure that you have access to session value and clear it when script is done, you can do something like this
Add file FlashMessages.php
with following content
FlashMessages.php
<?php
class FlashMessages {
private static $messages;
private static $sessionStarted = false;
public function __construct() {
if (!self::$sessionStarted) {
session_start();
self::$sessionStarted = true;
}
}
public function getMessage($name) {
if (!array_key_exists($name, self::$messages)) {
self::$messages[$name] = array_key_exists($name, $_SESSION) ? $_SESSION[$name] : null;
unset($_SESSION[$name]);
}
return self::$messages[$name];
}
}
In the place where you need to do your checking, add next code
<?php
require_once('FlashMessages.php');
$flashMessages = new FlashMessages();
switch ($flashMessages->getMessage('recent')) {
//...your code goes here
}
So I found my own solution (all of the above didn't work for me...):
in my code I would check for errors like this:
if (!$_SESSION['uid'] or !$_SESSION['login']) {
$_SESSION['recent'] = 'not_loggedin';
header('location: http://my.domain.com/');
}
however, there is al lot of other code following that chunk. I thought that the header
statement would stop the following code from being executed, but this does not seem to be the case, changing that code to the following works for me:
header
if (!$_SESSION['uid'] or !$_SESSION['login']) {
$_SESSION['recent'] = 'not_loggedin';
header('location: http://my.domain.com/');
die();
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Does this file have
session_start();
at the start of the PHP file? Also, instead of $_SESSION['recent'] = null, you can just unset the variable,unset($_SESSION['recent'])
.– vivek_23
3 hours ago