0

On my site, the login.php page (if successful login) will redirect to index.php and will start a session and 2 SESSION variables.

One of the variables started is a success message:

$_SESSION["message"] = "Login successful!";

the second is the user session variable:

$_SESSION["authenticatedUserEmail"] = $email;

the problem is that if I check the variables individually and then try and use them on the index.php page, only the first one that is checked will work.

This following snippet will show the $form_message but it will not show the $_SESSION["authenticatedUserEmail"]:

session_start();
if(isset($_SESSION["message"])) {
    $form_message = $_SESSION["message"];
    session_unset($_SESSION["message"]);
    echo $form_message;
} else {
    $form_message = "";
}


if (isset($_SESSION["authenticatedUserEmail"])) {

    echo $_SESSION["authenticatedUserEmail"];

}

It does work when I only use one if(isset($_SESSION statement but I don't want to always include both inside the same statement.

I've done an error check:

ini_set('display_errors',1); 
error_reporting(E_ALL); 

but no errors appear.

Can anyone please suggest why this may not be working or if I am missing something?

Thanks in advance.

2 Answers 2

4

session_unset function free all session variable. this is why when you are using session_unset, next session variable is not founded. read the manual please.

http://php.net/manual/en/function.session-unset.php

to achieve what you want you can use unset function

unset($_SESSION["message"]);

hope this helps

Sign up to request clarification or add additional context in comments.

Comments

2

Your call to session_unset is the problem, you should be simply using unset.

session_unset unsets the whole $_SESSION array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.