CodeIgniter Forums

Full Version: Using $_SESSION vs $this->session
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Question: Is using $_SESSION more efficient than using CI session class?

It would seem that using $_SESSION may be more efficient as it is already available to the controller, models and views.
However, if using the CI session class, the session library would need to be loaded in the constructor of every class referencing the session variables.

Is this accurate?
(09-30-2016, 03:18 PM)Cannondale Wrote: [ -> ]Question: Is using $_SESSION more efficient than using CI session class?

It would seem that using $_SESSION may be more efficient as it is already available to the controller, models and views.
However, if using the CI session class, the session library would need to be loaded in the constructor of every class referencing the session variables.

Is this accurate?

The session library can be autoloaded in the config so that it's always available.

https://www.codeigniter.com/user_guide/l...sions.html
https://www.codeigniter.com/user_guide/g...oader.html
If you don't use CI_Session then you will have to write your own code managing PHP sessions. $_SESSION isn't available until some code in your app makes a call to PHP's session_start(). Why reinvent the wheel?

CI versions > 3.0.x manage PHP sessions quite well and add some handy additional functionality. CI sessions can be autoloaded by using CI's autoload.php config file removing the need to load the library in multiple controllers. Although loading the library only where needed doesn't involve a lot of work either.

Most of the setting and retrieval of session data in the session library (version >= 3.0.0) is working directly with the $_SESSION variable. The retrieval functions $this->session->item and $this->session->userdata('item') have an advantage over $_SESSION['item'] in that the class functions return NULL if the item does not exist in the $_SESSION array. Directly reading from $_SESSION means you need to make sure the item exists before trying to access the array. Now that gets tedious.

The advantage of using a framework like CI is that a lot of that kind of tedium is eliminated. Why not use what the framework provides?
(09-30-2016, 06:52 PM)dave friend Wrote: [ -> ]If you don't use CI_Session then you will have to write your own code managing PHP sessions. $_SESSION isn't available until some code in your app makes a call to PHP's session_start(). Why reinvent the wheel?

CI versions > 3.0.x manage PHP sessions quite well and add some handy additional functionality. CI sessions can be autoloaded by using CI's autoload.php config file removing the need to load the library in multiple controllers. Although loading the library only where needed doesn't involve a lot of work either.

Most of the setting and retrieval of session data in the session library (version >= 3.0.0) is working directly with the $_SESSION variable. The retrieval functions $this->session->item and $this->session->userdata('item') have an advantage over $_SESSION['item'] in that the class functions return NULL if the item does not exist in the $_SESSION array. Directly reading from $_SESSION means you need to make sure the item exists before trying to access the array. Now that gets tedious.

The advantage of using a framework like CI is that a lot of that kind of tedium is eliminated. Why not use what the framework provides?


Thanks for your detailed reply. It helped clarify the advantages of using the CI session class.
Yes, accessing $_SESSION directly is technically more efficient.

(09-30-2016, 06:52 PM)dave friend Wrote: [ -> ]The retrieval functions $this->session->item and $this->session->userdata('item') have an advantage over $_SESSION['item'] in that the class functions return NULL if the item does not exist in the $_SESSION array. Directly reading from $_SESSION means you need to make sure the item exists before trying to access the array. Now that gets tedious.

The advantage of using a framework like CI is that a lot of that kind of tedium is eliminated. Why not use what the framework provides?

I'm not saying that this is false, but it is highly subjective.

You shouldn't use what the framework provides for its own sake, you should use it if it helps you.