22

I have a third party script and was wondering how I can check with PHP if session_start() has been declared before doing something?

something like if(isset($_session_start())) { //do something } 
Farshid Shekari
  • 2,391
  • 4
  • 27
  • 47
iaddesign
  • 231
  • 1
  • 2
  • 5

10 Answers10

42
if(!isset($_SESSION)) {
     session_start();
}

The check will allow you to keep your current session active even if it's a loop back submission application where you plan to reuse the form if data is typed incorrectly or have additional checks and balances within your program before proceeding to the next program.

Maarek
  • 682
  • 6
  • 6
  • 10
    i'd reccomend to use `if(session_id() == '')`, since `$_SESSION` can be set while the session is closed. – JimmyMcHoover Dec 18 '12 at 20:54
  • 1
    True, but all $_SESSION['variables'] are set to null when the session starts. Also, session_id()s are client side cookie values. It's up to the developer how session data is utilized. – Maarek Dec 02 '13 at 22:12
  • 1
    @Maarek Every time `session_start()` is called, the session variables ARE NOT all set to null. And when a session is closed (by exiting the script or by calling `session_write_close()`) the session data still persists. In such cases your check is not sufficient, because it will not restart a closed session. – Stefan Dec 15 '16 at 13:42
  • 1
    Also, you can call `session_start()` and then never set `$_SESSION`. Therefore your check will tell you that `session_start()` was never called, which is incorrect. – Stefan Dec 15 '16 at 14:19
  • A useful answer if there's only one session and not much advanced it done with it, but not great for more complex environments. – lilHar Oct 26 '18 at 19:42
  • In PHP version above 5.4.0 DON'T USE ```if(!isset($_SESSION)) session_start();``` or ```if(session_id() === "") session_start();```. They will not work properly after a call to ```session_write_close()``` & continue to report, that the session exists. Instead use ```if(session_status() !== PHP_SESSION_ACTIVE)``` & ```if(session_status() === PHP_SESSION_NONE)``` or compare to INT numbers i.e. _ACTIVE = 2, _NONE=1 and _DISABLED=0. Alternatively, if you have to, call ```@session_start();``` to supress "A session had already been started - ignoring session_start()" warning. – webcoder.co.uk Nov 11 '21 at 10:59
34

As in PHP >= 5.4.0, you have function session_status() that tell you if it have been initialize or not or if it's disabled.

For example you can do:

if (session_status() == PHP_SESSION_NONE) {
  session_start();
}

You can read more about it in http://www.php.net/manual/en/function.session-status.php

Al.G.
  • 4,327
  • 6
  • 31
  • 56
PhoneixS
  • 10,574
  • 6
  • 57
  • 73
13

I suppose you could use the session_id function :

session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).

Or maybe testing whether $_SESSION is set or not, with isset, might do the trick, as it should not be set when no session has been started -- you just have to hope that nothing assigns anything to $_SESSION without starting the session first.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Use session_status() instead of session_id() from PHP 5.4.0. The $_SESSION won't work properly after calling a session_write_close() function. – webcoder.co.uk Nov 11 '21 at 15:32
9

for php >= 5.4.0

if (session_status() == PHP_SESSION_NONE) {
session_start();
}

for php < 5.4.0

if(session_id() == '') {
session_start();
}
krishnaTORQUE
  • 187
  • 1
  • 3
  • 10
  • 1
    This is the recommended method. [http://www.php.net/manual/en/function.session-status.php](http://www.php.net/manual/en/function.session-status.php) – Gui Imamura May 21 '15 at 17:08
  • Note that the < 5.4.0 solution is pretty much useless. session_id() will always return an empty string until session_start() is called. It's called every time. – st_efan Feb 23 '16 at 11:34
2
if(isset($_SESSION)) {
    // do something
}
Rob
  • 8,042
  • 3
  • 35
  • 37
1
function session_started(){ return !!session_id(); }
0

If session already started, Error: "Fatal error: Can't use function return value in write context"

Try this:

$session = session_id();
if(empty($session)){ 
    session_start();
}
20AMax
  • 426
  • 5
  • 7
0

I just wrote a simple function for it.

  function isSessionStart ()
  {
    if (version_compare(phpversion(), '5.4.0', '<')) {
      if(session_id() == '') {
        return false;
      }
      return true;
    }
    else {
      if (session_status() == PHP_SESSION_NONE) {
        return false;
      }
      return true;
    }
  }
Yahya
  • 706
  • 8
  • 23
-1

best way work for me! if(session_status() != 1) session_start();

Abolfazl Miadian
  • 3,099
  • 2
  • 19
  • 15
-1

    if(defined('SID'))
        // do
Romka
  • 76
  • 5