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