Check if PHP session has already started

Check if PHP session has already started

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Check if PHP session has already started

The pattern of checking $_COOKIE["PHPSESSID"] before calling session_start() is a common workaround, but it relies on an assumption that isn't always true: that a session cookie exists only after session_start() has been invoked successfully. While this logic often prevents the "Session already started" error, it introduces fragility. If your server configuration disables cookies or if the browser blocks them, the check fails silently, and you end up starting a new session when one was intended to be resumed. Furthermore, relying on $_SESSION after a failed startup can lead to warnings about undefined variables, as seen in your error log.

The most robust way to determine if a session is already active is to inspect the session.status configuration value provided by PHP itself, rather than guessing based on the presence of a cookie. You should access this via the $GLOBALS['HTTP_SESSION_VARS'] (deprecated) or, more reliably in modern contexts, by checking the $_SESSION superglobal's existence within the current scope, but there is an even better approach that avoids ambiguity entirely: using session_status().

PHP provides a dedicated function specifically for this purpose: session_status(). It returns one of three constants: PHP_SESSION_DISABLED, PHP_SESSION_NONE, or PHP_SESSION_ACTIVE. This eliminates the need to parse cookies or guess based on variable existence.

Here is how you should structure your initialization logic:

if (PHP_SESSION_ACTIVE !== session_status()) {
    session_start();
}

This check is safe to run multiple times in a single request. If a session was already started by a previous script or a parent controller, session_status() will return PHP_SESSION_ACTIVE, and the code block will be skipped. If no session exists, it starts one. This approach handles edge cases where cookies are missing or disabled more gracefully than checking $_COOKIE.

However, there is a significant architectural consideration regarding how you structure your application. The warning "Notice: Undefined variable: _SESSION" typically occurs when code attempts to access $_SESSION immediately after calling session_start() that failed or was unnecessary in a specific context, or if the session was deleted mid-request. In modern frameworks, this pattern of manually checking session status often indicates a lack of a centralized bootstrap layer.

If you are maintaining legacy PHP code where you cannot enforce a strict entry point for every request, using session_status() is the correct fix. However, if you have the ability to refactor your application architecture, consider adopting a framework like Laravel or Symfony. These frameworks handle session initialization automatically during their bootstrapping phase, ensuring that $_SESSION is always available and properly initialized before any controller logic runs. This removes the need for manual status checks in individual scripts entirely.

For example, in a Laravel application, the framework's service container ensures that session data is loaded and accessible via $request->session() without you needing to worry about the underlying session_start() mechanics or checking cookie states. While this might seem like overkill for a simple script, it prevents the class of errors you are currently facing by enforcing a consistent lifecycle for sessions across your entire codebase.

Ultimately, switching to session_status() resolves the immediate warning and error issues without requiring a full framework migration. It respects PHP's internal state rather than inferring it from external artifacts like cookies. This ensures that your script behaves predictably regardless of whether the user has cookies enabled or whether the session was initiated by a previous request in the same flow.

Our network