2

Possible Duplicate:
how do i check if session_start has been entered?

Is there such an API?

Community
  • 1
  • 1
user198729
  • 61,774
  • 108
  • 250
  • 348

1 Answers1

7

You can find out by calling the following code:

if(isset($_SESSION))
{
    //The session was started
}
Sam Becker
  • 19,231
  • 14
  • 60
  • 80
  • 3
    Keep in mind that `session_write_close()` ends the session mechanism but doesn't destroy _SESSION. – VolkerK Jan 04 '10 at 08:27
  • This answer is not correct. As VolkerK points out, `$_SESSION` can still be set even though the session has been closed. And nothing prevents a user from declaring $_SESSION without starting a session. A better check would be: `if (session_id())` (For PHP versions < 5.4.0) or `if (session_status() != PHP_SESSION_NONE)` (For PHP versions >= 5.4.0) – Stefan Dec 15 '16 at 13:47
  • Also, you can call `session_start()` without ever setting $_SESSION. Your check will then tell you that `session_start()` was never called, which is incorrect. – Stefan Dec 15 '16 at 14:17