CodeIgniter Forums
How to tell if session loaded? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: How to tell if session loaded? (/thread-71712.html)



How to tell if session loaded? - skunkbad - 09-15-2018

Sorry if this seems like a stupid question, but I'm not actually playing with CI4, so I can't dig through code, debug, and figure things out on my own.

From the docs:


Quote:Sessions will typically run globally with each page load, so the Session class should be magically initialized.



The part that I'm concerned with is "should be". If the session is already initialized, would calling session() a second time mess things up?

PHP Code:
$session = \Config\Services::session(); 

In CI3 I could do this: 

PHP Code:
if( ! $this->load->is_loaded('session') )
    
$this->load->library('session'); 

But in CI4, since session() takes a config param, do I worry about calling session() a second time and creating a conflict? How would I know that $session is initialized?


RE: How to tell if session loaded? - kilishan - 09-16-2018

Where do you see that in the docs? That should be eradicated. The Session library is pretty much a straight port of the CI3 one, so it should be pretty much what you're used to.

The session is not started until you load it the first time. Calling the session() helper command will automatically start it if it isn't already started.


RE: How to tell if session loaded? - skunkbad - 09-16-2018

(09-16-2018, 11:44 AM)kilishan Wrote: Where do you see that in the docs? That should be eradicated. The Session library is pretty much a straight port of the CI3 one, so it should be pretty much what you're used to.

The session is not started until you load it the first time. Calling the session() helper command will automatically start it if it isn't already started.

This is where I see it:

https://bcit-ci.github.io/CodeIgniter4/libraries/sessions.html#initializing-a-session

but I'd still like to know the CI4 way to tell if something like the session is already loaded.


RE: How to tell if session loaded? - kilishan - 09-16-2018

I don't believe the $_SESSION variable is available until the session has been initiated, IIRC. So you could do isset($_SESSION).

Or you could not worry about checking, since as long as you use the session() helper method, it will be automatically started on the first use. Trying to grab a variable like with $key = session('some_key') will ensure the session is started before grabbing the value.


RE: How to tell if session loaded? - skunkbad - 09-16-2018

(09-16-2018, 02:43 PM)kilishan Wrote: I don't believe the $_SESSION variable is available until the session has been initiated, IIRC. So you could do isset($_SESSION).

Or you could not worry about checking, since as long as you use the session() helper method, it will be automatically started on the first use. Trying to grab a variable like with $key = session('some_key') will ensure the session is started before grabbing the value.

If I'm not mistaken, this line in the session method may point to the right way to check:


PHP Code:
if (session_status() == PHP_SESSION_NONE



RE: How to tell if session loaded? - InsiteFX - 09-17-2018

Hi guys,

I check it like this, according to PHP.net

PHP Code:
// Ensure session is started and running
if (session_status() == PHP_SESSION_NONE)
{
 
   // session has not started so start it
 
   session()->start();

See these:
PHP.net - session_status
PHP.net - Session Functions