Session content not being deleted with either session_destroy(); or unset

Multi tool use
Multi tool use


Session content not being deleted with either session_destroy(); or unset



I've had this issue for a couple of days and I can't seem to fix it whatever I try.



There is a loginform that either redirects you to /dashboard or /admin depending on the data that is in the started session from my loginscript.


/dashboard


/admin



This is my structure:


index.php (contains header with connection.php and the html login form)
dashboard.php (contains header with connection.php and some irrelevant html)
admin.php (same as above)
header.php (at the top of all pages, has connection.php included at the first line).
connection.php (my connection script, with at the top line session_start();
login.php (my loginscript, this script is called with ajax so I have to add my entire connection class in here with session_start(); again at the top.
logout.php (my logout script, it kills the session and redirects to login page this is linked to in my header.php)



In my loginscript I have the following part:


if($userpassword == $getuser['password']){
if($getuser['rights'] == '1'){
$_SESSION['userdata']['user'] = 'Administrator';
$_SESSION['userdata']['rights'] = '1';
$_SESSION['userdata']['logo'] = 'assets/images/logo.png';
$loginresult = array(
'login_result' => 'success',
);
$logindata = array(
'userdata' => 'admin',
);
echo json_encode($logindata);
}else{
$_SESSION['userdata']['user'] = $getuser['name'];
$_SESSION['userdata']['rights'] = '0';
$_SESSION['userdata']['logo'] = $sessionlogo;
$loginresult = array(
'login_result' => 'success',
);
$logindata = array(
'userdata' => 'user',
);
echo json_encode($logindata);
}
}else{
$logindata = array(
'userdata' => '',
'message' => 'Wachtwoord en gebruikersnaam komen niet overeen',
);
echo json_encode($logindata);
}



In short: when a user logs in as admin they see the name Administrator and when they login with another account from my database, they see that particular name.


Administrator



My logout.php:


<?php
session_start();
unset($_SESSION['userdata']);
header("Location: http://website.nl/login");
die();
?>



I have also tried session_destroy(); instead of unset


session_destroy();


unset



Now when I login with an admin for example, it works fine, but if I then logout and login with a user, I am redirected to the correct page but I still see the name Administrator even though I'm not? This works the other way around too.


Administrator



So a session is set and filled with data, but on logging out and logging back in, that session is not replaced/deleted, it just keeps existing.



Why is that? The only thing that I can think of is my login.php that is called with ajax. It is the only place where another session_start(); is present, but if I remove it my loginscript doesn't work anymore.





Do you add any parameters when starting the session somewhere or is it always just a session_start() with nothing more ? Checking your session in the browser for example. Do you see two of them or just one?
– Sourcey86
Jul 2 at 9:58






No parameters, always just session_start(); How can I check a session in my browser? If I google for more information on that I see you can only do that with cookies.
– twan
Jul 2 at 10:09


session_start();





This might not be an actual session problem, but rather one with browser caching. What happens when you refresh using [f5] when you are on a page where you see such a “wrong” value?
– CBroe
Jul 2 at 10:11






@CBroe Still the same issue, even with a hard refresh (ctrl shift r). But I found where to view my sessions (what @Sourcey86 was talking about) and yes when I login I have two active sessions. One on domain www.mysitename.nl and one mysitename.nl so with www. and without. I wonder why that is.
– twan
Jul 2 at 10:14


www.mysitename.nl


mysitename.nl





You can also work around that by specifying the cookie domain for the session cookie so that it “covers” subdomains automatically. php.net/manual/en/…, php.net/manual/en/function.session-set-cookie-params.php
– CBroe
Jul 2 at 10:21









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.

7yn 3P66 hpl7Tw0TWl23o MdsOBZlThjP,qT,bzFlLwsZANh4EFBy,gY5b C,i3kh,I6Q,FwXCV3b03Ok3GL0uH9PT
3CcQUg V 5FCBA3gbG0WsFMiuBOIlp5FL,khCdAQWcacwVuFRPerokZYflHToywc J e SWvcmTTw3KdWJmhKvOMJchMD cCB1ME,nC

Popular posts from this blog

Rothschild family

Cinema of Italy