session – Determine if $_SESSION superglobal exists in PHP-ThrowExceptions

Exception or error:

I want to do something like this:

if ($_SESSION['errors'] exists)
{
    //Do stuff
}

I want to create a session on page1 and then go to page2 where it will check for errors, if there are errors it returns to page1 with the errors.

But page1 will give errors if the variable hasn’t been created yet on page 2.

If I do $_SESSION['errors'] == "" on page1 it will reset the variable so that’s no good.

How to solve:
if (isset($_SESSION['errors']))
{
    //Do stuff
}

Answer:

use isset() and empty() php function.

if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])) {
    // ...
} 

Answer:

if (!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
     // do stuff
}

Leave a Reply

Your email address will not be published. Required fields are marked *