How do i check if session_start has been entered? [duplicate]
Asked Answered
php
W

10

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 } 
Washington answered 24/11, 2009 at 6:24 Comment(4)
Yes, but this was asked 3 years ago. That question was asked 2 years ago.Konopka
Refer to Check if PHP session has already startedNitrite
Check this outGermano
I just always do @session_start(); and who cares - one line - if it is started, you don't care... if it isn't, well it is now. Is that wrong?Infelicity
D
43
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.

Distributor answered 22/2, 2011 at 14:45 Comment(6)
i'd reccomend to use if(session_id() == ''), since $_SESSION can be set while the session is closed.Bak
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.Distributor
@Distributor 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.Hamrah
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.Hamrah
A useful answer if there's only one session and not much advanced it done with it, but not great for more complex environments.Heinous
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.Hodess
R
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

Remark answered 24/7, 2012 at 10:50 Comment(0)
H
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.

Hootchykootchy answered 24/11, 2009 at 6:29 Comment(1)
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.Hodess
L
9

for php >= 5.4.0

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

for php < 5.4.0

if(session_id() == '') {
session_start();
}
Lysozyme answered 19/2, 2014 at 15:11 Comment(2)
This is the recommended method. http://www.php.net/manual/en/function.session-status.phpPenicillate
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.Parable
L
2
if(isset($_SESSION)) {
    // do something
}
Leo answered 24/11, 2009 at 6:27 Comment(0)
A
1
function session_started(){ return !!session_id(); }
Alibi answered 6/12, 2009 at 17:4 Comment(1)
Don't be too clever ;). I'd prefer 'return (session_id() !== "")'.Stenopetalous
R
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();
}
Ringtailed answered 3/8, 2014 at 20:26 Comment(0)
G
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;
    }
  }
Germano answered 30/7, 2017 at 7:36 Comment(0)
P
-1

    if(defined('SID'))
        // do
Pintle answered 28/4, 2011 at 8:49 Comment(0)
F
-1

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

Finnell answered 24/11, 2017 at 18:18 Comment(1)
1 mean still session is not started, its in none position.Prestonprestress

© 2022 - 2024 — McMap. All rights reserved.