Alex Rivera | Logout

Determine if $_SESSION superglobal exists in PHP

Asked 2012-06-01T19:05:54.097
31

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.

Edit
Report

1 Answer

10

use isset() and empty() php function.

if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])) {
    // ...
} 
answered 2012-06-01T19:10:08.133

Your Answer