As you probably already know, session_start is an in-built PHP function that will start a new session.

The problem is:

  • If you call it more than once, your script will throw an E_NOTICE error.
  • Although the solution seems pretty straight forward (don’t call it more than once), in certain scenarios, you won’t be entirely sure if a session has already been started or not.
  • In some cases, it might be out of your control.
  • There are two ways to approach this.

If you are using a PHP version that is lower than 5.4.0, you can do the following:

[pastacode lang=”php” manual=”%3C%3Fphp%0Aif(session_id()%20%3D%3D%20”)%7B%0A%20%20%20%20%2F%2Fsession%20has%20not%20started%0A%20%20%20%20session_start()%3B%0A%7D%0A%20%0Avar_dump(session_id())%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • If you run the code above, you’ll see that a session is always started. This is because:
  • We check to see if the function session_id returns an empty string.
  • If session_id returns an empty string, we can conclude that the session has not been started yet.
  • If this is the case, we can start the session by calling the function session_start.
  • In PHP version 5.4.0 and above, we can make use of the function session_status, which returns the status of the current session.
  • This function can return three different integer values, all of which are available as predefined constants.

These are:

[pastacode lang=”php” manual=”%3C%3Fphp%0Aif(session_status()%20%3D%3D%20PHP_SESSION_NONE)%7B%0A%20%20%20%20%2F%2Fsession%20has%20not%20started%0A%20%20%20%20session_start()%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

[ad type=”banner”]

As you can see, using this function makes your code a little more self-explanatory!

Note that you can also check to see if the $_SESSION array has been set. However, it is worth noting that $_SESSION can be manually initialized like so:

[pastacode lang=”php” manual=”%3C%3Fphp%0A%24_SESSION%20%3D%20array(%0A%20%20%20%20’test’%20%3D%3E%20true%0A)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

i.e. The $_SESSION array might exist, even if a session hasn’t already been started.

[pastacode lang=”php” manual=”if%20(version_compare(phpversion()%2C%20’5.4.0’%2C%20’%3C’))%20%7B%0A%20%20%20%20%20if(session_id()%20%3D%3D%20”)%20%7B%0A%20%20%20%20%20%20%20%20session_start()%3B%0A%20%20%20%20%20%7D%0A%20%7D%0A%20else%0A%20%7B%0A%20%20%20%20if%20(session_status()%20%3D%3D%20PHP_SESSION_NONE)%20%7B%0A%20%20%20%20%20%20%20%20session_start()%3B%0A%20%20%20%20%7D%0A%20%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

Prior to PHP 5.4 there is no reliable way of knowing other than setting a global flag.

Consider:

[pastacode lang=”php” manual=”var_dump(%24_SESSION)%3B%20%2F%2F%20null%0Asession_start()%3B%0Avar_dump(%24_SESSION)%3B%20%2F%2F%20array%0Asession_destroy()%3B%0Avar_dump(%24_SESSION)%3B%20%2F%2F%20array%2C%20but%20session%20isn’t%20active.%0A” message=”Php Code” highlight=”” provider=”manual”/] [pastacode lang=”php” manual=”session_id()%3B%20%2F%2F%20returns%20empty%20string%0Asession_start()%3B%0Asession_id()%3B%20%2F%2F%20returns%20session%20hash%0Asession_destroy()%3B%0Asession_id()%3B%20%2F%2F%20returns%20empty%20string%2C%20ok%2C%20but%20then%0Asession_id(‘foo’)%3B%20%2F%2F%20tell%20php%20the%20session%20id%20to%20use%0Asession_id()%3B%20%2F%2F%20returns%20’foo’%2C%20but%20no%20session%20is%20active.%0A” message=”Php Code” highlight=”” provider=”manual”/]

[ad type=”banner”]

Sample code:

[pastacode lang=”php” manual=”if%20(!isset(%24_SESSION))%20session_start()%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

Categorized in: