Sessions, how? |
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...ht=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'?
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
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.
I start it in the BaseController:
PHP Code: <?php What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |