I'm working on my own learning project for fun, and have been banging my head against the wall for the last few weeks. I have session variables that simply don't exist when they should and vice versa.
On "example.com/dashboard" , $_SESSION['user']
exists, and then when going to "example.com/app/list" there's an if-statement testing if it does exist and is set.
<?php
session_start();
if(!isset($_SESSION['user'])) {
$_SESSION['errorMessage'] = "You are not logged in yet!";
header('Location: /');
exit;
}
Even though I verified $_SESSION['user']
is set on example.com/dashboard, the if-statement returns false and throws me to the login page.
*In my case the login page is my index.php on the root of my website.
Now, on this login page you should be immediately redirected to the dashboard if $_SESSION['user']
is set.
<?php
session_start();
if(isset($_SESSION['user'])) {
header('Location: /dashboard');
exit();
}
Like I said before, it is set, but somehow isn't. When refreshing, it sends me to the dashboard (which is supposed to happen if $_SESSION['user']
is set.)
That is problem one.
Now on "example.com/register", whilst not being logged in (Session was destroyed on logout using session_unset, session_destroy etc.), $_SESSION['user']
does not exist. After pressing submit, somehow $_SESSION['user']
is set to what it used to be when you were logged in. Absolutely nowhere else is there a piece of code that sets $_SESSION['user']
to anything, except for the login script. There is nothing on my register page that would allow this. It is as if after logout the session is retained.
Code from my root page aka login form (index.php
):
<?php
session_start();
if(isset($_SESSION['user'])) {
header('Location: /dashboard');
exit();
}
Code from dashboard.php
:
<?php
session_start();
if(!isset($_SESSION['user'])) {
$_SESSION['errorMessage'] = "You are not logged in yet!";
header('Location: /');
exit;
}
Code from my logout.php
(Since it might have to do something with this):
<?php
session_start();
if(!isset($_SESSION['user'])) {
$_SESSION['errorMessage'] = "You are not logged in yet!";
header("Location: ../");
die();
}
//clear session from globals
$_SESSION = array();
session_unset();
session_destroy();
unset($_SESSION);
header("Location: ../");
die;
On my register.php
:
<?php
session_start();
//register stuff ^^^^^
//eventually, mail to registerer
if (mail($to, $subject, $htmlContent, $headers)) {
$connect->query($sqlInsert);
$_SESSION['register_success'] = true;
header("Location: https://example.com/register-success");
exit;
}
Then on my register-success.php
:
session_start();
if(!isset($_SESSION['register_success'])) {
$_SESSION['errorMessage'] = "An error has occured, please try again.";
header("Location: https://example.com/register");
exit;
}
Keep in mind that whenever I submit the registration, I do receive the email so $_SESSION['register_success']
should be set, but still throws false at the if statement on "example.com/register-success"
Things that I have tried:
session_start();
at the begin of each script.session_destroy()
,session_unset()
,$_SESSION = array()
,unset($_SESSION)
at logout, and then a combination of all of them.- Checking if all session variable names match, which I have made sure to do.
I have absolutely no idea why this is happening.
Apologies in advance if I have explained it terribly.
Thank you for reading.