• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to tell if session loaded?

#1
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?
Reply

#2
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.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#3
(09-16-2018, 12:44 PM)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/l...-a-session

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

#4
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.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#5
(09-16-2018, 03: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
Reply

#6
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
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.