Current Location: Home> Latest Articles> PHP Session Check Guide: How to Determine if a Session Has Started

PHP Session Check Guide: How to Determine if a Session Has Started

M66 2025-09-19

Checking if a Session Has Started in PHP

In PHP, we use the built-in function session_start() to start a session. However, calling this function more than once in the same script can cause errors. Therefore, it is recommended to check whether a session has already started before executing session_start().

Method One: For PHP Versions Below 5.4.0

For older PHP versions, you can determine whether a session has started by checking if session_id() is empty.

<?php
if(session_id() == ''){
    session_start();
}
?>

Explanation

If the session has not started, the code above will start it, ensuring that session_start() is not called multiple times.

Method Two: For PHP 5.4.0 and Above

From PHP 5.4.0 onwards, you can use the session_status() function to get the current session status. This function returns the following three constants:

  • PHP_SESSION_DISABLED: Sessions are disabled
  • PHP_SESSION_NONE: Sessions are enabled but not started
  • PHP_SESSION_ACTIVE: Sessions are enabled and started
<?php
if(session_status() == PHP_SESSION_NONE) {
    session_start();
}
?>

Explanation

The code checks whether session_status() equals PHP_SESSION_NONE and starts the session if it hasn't started yet, preventing errors from calling session_start() multiple times.

Notes

The session_status() function used in Method Two is only available in PHP 5.4.0 or higher. For lower versions, Method One should be used.