1

I have a PHP Class i'm in the process of creating, which will work with the $_SESSION Super global, but thinking a little further into the working environment. I have decided not to use the __construct to start the session when the class is called, but left it to: $Class->init();.

I want the class to have the ability to migrate to a webpage which has already called session_start... Again, back to leaving the session_start() out of the constructor function. My Code is as followed:

class Session { 
        protected $Session_Started = false; 

    public function init(){
        if ($this->Session_Started === false){
            session_start();
            $this->Session_Started = true;
            return true;
        }
        return false;
    }
    public function Status_Session(){
        $Return_Switch = false; 
        if (session_status() === 1){
            $Return_Switch = "Session Disabled";
        }elseif (session_status() === 2){
            $Return_Switch = "Session Enabled, but no sessions exist";
        }elseif (session_status() === 3){
            $Return_Switch = "Session Enabled, and Sessions exist";
        }
        return $Return_Switch;
    }
   /*Only shown necessary code, the entire class contents is irrelevant to the question topic */

With the code being shown.. It's clear i'm validating if the session has been called previously by two methods, the internal reference: $this->Session_Started which is equal to true or false

and i'm also calling session_status() and validating the response.

A little earlier, I said I want it to migrate to sites that might have already called session_start(), What would be the best approach to validating if the session has already been called?.. The last thing I want this class to do, is start throwing errors upon import and initializing the class

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
  • possible duplicate http://stackoverflow.com/questions/6249707/check-if-php-session-has-already-started – bitWorking Aug 15 '13 at 14:28
  • Incorrect, that question is talking about the validation out of oop scope, im talkinabout,using an internal reference for session validations, – Daryl Gill Aug 15 '13 at 14:44

1 Answers1

1

You need to combine it with a "session has started" check.

public function init()
{
    if ($this->Session_Started) {
        return true;
    }

    if (session_status() === PHP_SESSION_ACTIVE) {
        $this->Session_Started = true;
        return true;
    }

    if ($this->Session_Started === false) {
        session_start();
        $this->Session_Started = true;
        return true;
    }
    return false;
}

Or in the constructor:

public function __construct()
{
    if (session_status() === PHP_SESSION_ACTIVE) {
        $this->Session_Started = true;
    }
}
bitWorking
  • 12,485
  • 1
  • 32
  • 38
  • Looking at this, makes me think to use the satus_session function within the construct, and start session if the returns === 0, sound like a logical thing to do? – Daryl Gill Aug 15 '13 at 14:57
  • I have a similar session class and I only start the session when a session is to be written/readed (lazy loading) – bitWorking Aug 15 '13 at 15:06
  • @redraggae sorry for the delay for the upvote/accept, was at work. This has helped me solve my little block – Daryl Gill Aug 15 '13 at 20:24