You want to know if a PHP session is active or if a specific variable exists in it. Here is the direct answer for checking a variable:

<?php
session_start();

if (isset($_SESSION['user_id'])) {
    // The variable exists and is not null
}

If you just need to know if any session is currently running, that is a different check. I cover both below because people mix them up constantly.


Check if a specific session variable exists

The standard way to check if a key exists in the $_SESSION superglobal is isset(). It returns true if the variable is set and its value is not null. Otherwise, it returns false.

I used to overcomplicate this. I’d write loops or try to catch errors from accessing undefined indexes. Don’t do that. isset() is atomic and safe. It does not trigger an “Undefined index” notice.

<?php
session_start();

// Safe check
if (isset($_SESSION['cart_id'])) {
    echo "Cart exists";
} else {
    echo "No cart yet";
}

There is a catch, though. isset() treats null as “not set.” If you store a value that can legitimately be null, isset() will lie to you. In that rare case, use array_key_exists().

<?php
// Use this if null is a valid state for the variable
if (array_key_exists('last_login_ip', $_SESSION)) {
    // Key exists, even if value is null
}

For 95% of cases, isset() is the right tool. It’s fast, readable, and idiomatic PHP.


Check if a session is active at all

Sometimes you don’t care about a specific variable. You just need to know: “Is there a session running right now?”

For that, use session_status(). It returns one of three constants: PHP_SESSION_NONE, PHP_SESSION_ACTIVE, or PHP_SESSION_DISABLED.

<?php
if (session_status() === PHP_SESSION_ACTIVE) {
    // A session is currently active
} else {
    // No active session
}

This is useful for middleware-style checks or when you’re debugging why $_SESSION is empty. If the session isn’t active, $_SESSION will be an empty array (or undefined), and all your isset() checks will fail regardless of what you think is stored.


Don’t use empty() for this

I see this mistake a lot. People use empty() to check if a session value exists.

<?php
// Dangerous for counters or flags
if (!empty($_SESSION['login_attempts'])) {
    // This fails when login_attempts is 0, which is a valid state!
}

empty() returns true for null, false, 0, "0", "", and an empty array. That’s fine for checking if a string is blank. It’s terrible for checking existence of a counter or a boolean flag.

If you’re checking a counter like login_attempts, use isset(). If you’re checking a name field, empty() might be okay. But don’t mix the two concepts in your head. Existence and emptiness are different questions.


A real example: login guard

Here’s how I actually use this in a small app. Every protected page starts with this:

<?php
session_start();

if (!isset($_SESSION['user_id'])) {
    header('Location: /login.php');
    exit;
}

// Continue rendering the page
echo "Welcome back, user #" . $_SESSION['user_id'];

Clean. No exceptions. No notices. Just a redirect if the user isn’t logged in.

If you want to pull the value with a default, use the null coalescing operator:

<?php
$userId = $_SESSION['user_id'] ?? null;

That’s it. No isset() needed here because you’re not branching on existence, just reading with a fallback.


Starting sessions safely

One last thing. session_start() should only be called once per request. If you call it twice, PHP throws a warning. To avoid that, check the status first:

<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

This pattern is safe to drop into any file that needs session access. I use it in my base template files so I don’t have to remember which pages already started the session.

If you’re new to sessions, I wrote a deeper post on creating sessions in PHP and another on deleting session data. Those cover the lifecycle beyond just checking existence.


Stop guessing, start checking

The difference between isset() and session_status() is small, but mixing them up causes confusing bugs. One checks a variable. The other checks the session itself.

Use isset($_SESSION['key']) for most cases. Use array_key_exists() if null is valid. Use session_status() to verify the session is live. And never use empty() unless you actually care about falsy values.

That’s the whole thing. No magic. Just clear checks that tell you exactly what’s in your session.