Welcome Guest, Not a member yet? Register   Sign In
A session on $_SESSION
#6

(This post was last modified: 07-06-2021, 12:24 PM by Gary. Edit Reason: Correcting alcohol-assisted typos. I'm sure there's still others. )

Thanks InsiteFX, MGatner... I've been experimenting with your suggestions over the last couple of evenings.

Prior to opening this thread, I was using the following in BaseController:
Code:
$session = \Config\Services:: session();

However, in addition to this, I also found it necessary (because they are not in the App\Controllers namespace) to also use the same in all my Filters, Libraries and also in the few places (where I've found it makes life happier) to use $session in the Models.

Generally, after providing the link to Services:: session() as above, I've used $_SESSION[' '] to access the PHP session variables directly (thinking it was as close to the bare basics as was feasible (also with the mindset of there being the least code changes in subsequent CodeIgniter upgrades)).

This has worked very well, without the slightest problem since I started playing with CodeIgniter 4, late 2019.

For what it's worth, because of running out of other things to try, I've also tried (doing pretty much the same thing this way) :
Code:
use \Config\Services;
....
$session = session();

What I found (after upgrading from v4.0.x to v4.1.3 and switching from a Windows to Linux, as mentioned in the first post) is that doing it this way works more than 99.9% of the time, but after an indeterminate (seemingly completely random) period, $_SESSION is lost.

This loss will typically happen in the first Filter that references $_SESSION (albeit that it's the last of the before Filters).

If the reference to $_SESSION is removed from that Filter, it will fail in the first Controller in the App\Controllers namespace (not the BaseController, which I suspect is called even before the Filters (?)).

I've experimented using InsiteFX's method, and have found that placing just this SINGLE session()->get() use in the Filter:
Code:
$session = session();
if (!empty(session()->get('user_session'))) {    // in place of !empty($_SESSION['user_session'])
appears to make everything work 100% reliably again, exactly as it previously was (the rest of the site still uses the $_SESSION[' '] reference)??!

The problem I now have is not knowing whether I can trust what seems to be a fundamental PHP construct in the CodeIgniter framework (albeit that the way I've been using it is 100% as per the recommendations in the User Manual (see:  https://codeigniter.com/user_guide/libra...%20use#id3 and https://codeigniter.com/user_guide/libra...ssion-data))... and whether it is perhaps better to switch to the session()->get() and ->set() methods throughout the whole project (not something I'd like to do, in part because there's some multi-dimensional arrays stuck among the $_SESSION variables (and functions that manipulate these arrays) in many places in the code (which, yes, will probably be simple enough to translate, but may make it more tedious than a simple text replacement, AND will mean actually having to think.  Please, NO!).

Does anyone have an idea of what could be causing the lost $_SESSION, and why using a single instance of session()->get() appears to fix it?

Also, is it safe to continue using $_SESSION?


On a separate note, I have made regular use of helpers (and have several custom helpers), but hadn't considered the Service routines as helpers (probably attributable to be a noob, and because they weren't listed under the Helpers in the User Manual).  Even the documentation on Services doesn't give one many clues that there is more to look at. For interest sake, session() is filed under 'Global Functions and Constants' in the User Manual (and I now see model() there too).

Some of those listed in your reply, I've not even aware of until now, thanks MGatner.



Update:

No... session()->get() appears to IMPROVE matter a whole lot, but $_SESSION is still being lost in the same place it was before.
Reply


Messages In This Thread
A session on $_SESSION - by Gary - 06-24-2021, 04:06 PM
RE: A session on $_SESSION - by superior - 06-25-2021, 07:50 AM
RE: A session on $_SESSION - by Gary - 06-26-2021, 03:26 PM
RE: A session on $_SESSION - by InsiteFX - 06-26-2021, 08:46 PM
RE: A session on $_SESSION - by MGatner - 06-27-2021, 04:52 AM
RE: A session on $_SESSION - by Gary - 07-03-2021, 01:57 PM
RE: A session on $_SESSION - by InsiteFX - 07-03-2021, 09:02 PM
RE: A session on $_SESSION - by Gary - 07-06-2021, 12:26 PM
RE: A session on $_SESSION - by Gary - 07-07-2021, 04:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB