![]() |
Sessions, how? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Sessions, how? (/showthread.php?tid=77498) |
Sessions, how? - blaasvaer - 09-08-2020 I seem to constantly having a hard time reading and actually USING the docs: Can someone tell med HOW, WHERE and WHEN to use this: Code: $session = \Config\Services::session($config); It's the first line of 'code' in the docs about the Session Library (https://codeigniter4.github.io/userguide/libraries/sessions.html?highlight=session) ... but I cannot – from the docs – figure out HOW to implement and USE it. If I put it into a Controller, I get this: Code: syntax error, unexpected '$session' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) Can somone explain why I'm getting this, and what it is that I seem to be constantly missing when 'reading' the docs? I see this a LOT in the docs '$variable = something … ', but WHENEVER I try to implement what I'm reading in the docs I ALWAYS get errors. Now, does: Code: $variable = ... in the docs have some 'magical' meaning (should it be 'read' in some particular way)? Or is it simply because the docs 'assume' YOU know where you're supposed to 'put' this code ... and therefore leaving out the (not so) 'obvious'? RE: Sessions, how? - captain-sensible - 09-08-2020 yes i struggle with the docs sometimes; what i do is get concepts working anyway i can then re-visit docs for more elegant ways of doing it. This is the start of one of my controller's : Code: public function credentials() first line of controller (and also any controller) that will pick up the ball will have as first line session_start(); I set a session array variable using $_SESSION then later Code: public function logout() this is part of a method to unset sessions I use this is basically old school php ; i'm using it on codeigniter4.0.4 mind you, when i say old school, $_SESSION is not that old since if you look inside SESSION.php file mine is at vendor/codeigniter4/framework/system/Session/Session.php there are a lot of references and use of $_SESSION RE: Sessions, how? - stlake2011 - 09-08-2020 Looking at your code, it looks like your not declaring the $session variable first. Heres a quick example: PHP Code: class Controller Or if you need/want sessions available everywhere, then uncomment the line: $this->session = \Config\Services: ![]() In the BaseController and extend your controllers off that. If you search a bit, I believe InSiteFX has a better way to implement them, by checking to see if the session is already active or not, posted here in the forums, just can't remember where. Hope this helps. RE: Sessions, how? - InsiteFX - 09-09-2020 I start it in the BaseController: PHP Code: <?php |