Simple, I don't understand why it doesn't enter inside the IF when closing the session and opening with a different user.
if (!isset($_SESSION['Username'])) {
$_SESSION['Username'] = $_SESSION['MM_Username'];
}
Only works the first time and remain with the same name even if I log out. I also closed the browser and I always get the same name. I thought that $_SESSION['Username']
would be destroyed after closing the session.
The code you have posted doesn't initiate the session:
session_start();
if (!isset($_SESSION['Username'])) {
$_SESSION['Username'] = $_SESSION['MM_Username'];
}
You need to tell apache to kick off session variables using session_start otherwise you can't access anything in it.
Additionally from memory, you will need to post this at the top of your script (before any data is sent to the user) - though this might not matter, I can't recall right now. Please edit if that statement is incorrect :)
Edit: If you have got this and it is functioning, you should use a session_destroy when logging the user out to ensure that any data is wiped out from the session. This will mean you won't have to worry about closing the browser or anything like that:
// your logout script... and
session_destroy();
If you want to unset all your $_SESSION variables you can do the following:
$_SESSION = array();
If you want to make sure that the session ends when your user logs out, use http://php.net/manual/en/function.session-destroy.php .
Unintended results may occur if you assume the session will end after a certain amount of time or when the user closes a browser tab.