How can you check if a PHP session exists?

php, session

Solution

In PHP versions prior to 5.4, you can just the `session_id()` function:

$has_session = session_id() !== '';

In PHP version 5.4+, you can use `session_status()`:

$has_session = session_status() == PHP_SESSION_ACTIVE;

Problem

Just wondering how to check if a PHP session exists... My understanding is that no matter what, if I am using sessions, I have to start my files with session_start() to even access the session, even if I know it already exists. I've read to user session_id() to find out if a session exists, but since I have to use session_start() before calling session_id(), and session_start() will create a new ID if there isn't a session, how can I possible check if a session exists?

Original source

Related problems