Search code examples
phpsessionmessagenotify

Use sessions for display notifications?


I want to show a message when the user logs in:

If the data is correct I will then create a user session and another session named CORRECT, then finally check if this session exists in order to show the correct message:

First I create the session:

$_SESSION['CORRECT'] = true;

And now I check:

if($_SESSION['CORRECT']){
    echo = "Correct";
    $_SESSION['CORRECT'] = false;
}else{
    echo = "Incorrect";
}

My question: is this the correct way to verify the existence of the session? or should I check it like this?:

if($_SESSION['CORRECT'] == true)

Solution

  • 1: if($_SESSION['CORRECT']){}

    2: if($_SESSION['CORRECT'] == true){}

    Both can be used for checking but they serve different purposes as

    if($_SESSION['CORRECT']){} will check that if($_SESSION['CORRECT']){} is present in the code or not and if($_SESSION['CORRECT'] == true){} will check whether the value of $_SESSION['CORRECT'] is equal to true.