CodeIgniter Forums

Full Version: Setting and Getting Session and Flashdata
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can anybody guide me how to set and retrieve session and flashdata?

I initialized session base controller and when I try to set and get on User controller, I am getting error saying "ErrorExceptoin": undefined variable $session when using this snippet below

PHP Code:
$session->set('user''someone'); 
$session = session();
$session->set('user', 'someone');
(09-26-2019, 01:35 AM)titounnes Wrote: [ -> ]$session = session();
$session->set('user', 'someone');

Thank you, it works

Should "$session = session();" be called on each controller?

Is there any way to autoload like CI3 version?
full simple example for CI4:

PHP Code:
<?php
namespace App\Models;

use \
Config\Services\session;

class 
My_Model
{
     private session null;
     
     
public function __construct() {
           $this->session = new session();
     }
     public function setDataSession($data) {
           // ... you code
           $this->session->set$data );
           // ... you another code
     }


Official guide to using the session library: User_Guide
(09-26-2019, 02:14 AM)Digital_Wolf Wrote: [ -> ]full simple example for CI4:

PHP Code:
<?php
namespace App\Models;

use \
Config\Services\session;

class 
My_Model
{
     private session null;
     
     
public function __construct() {
           $this->session = new session();
     }
     public function setDataSession($data) {
           // ... you code
           $this->session->set$data );
           // ... you another code
     }


It throws error Sad

(09-26-2019, 12:16 AM)msjagan Wrote: [ -> ]Can anybody guide me how to set and retrieve session and flashdata?

I initialized session base controller and when I try to set and get on User controller, I am getting error saying "ErrorExceptoin": undefined variable $session when using this snippet below

PHP Code:
$session->set('user''someone'); 
I was also facing same problem. I got my answer, thaks a lot
CI4, different with CI3.
learn about filter.
https://github.com/codeigniter4/CodeIgni...ilters.rst

use BaseConttoller for autoload libraries, helper etc.
Yes CI4 is way different than CI3.

learning the changes was made on CI4 by applying in sample project.

Also found simple way to set and get session and flashdata. I will share my code with sample project which I am working on once its done.
(09-26-2019, 02:53 AM)sktiwari19 Wrote: [ -> ]
(09-26-2019, 12:16 AM)msjagan Wrote: [ -> ]Can anybody guide me how to set and retrieve session and flashdata?

I initialized session base controller and when I try to set and get on User controller, I am getting error saying "ErrorExceptoin": undefined variable $session when using this snippet below

PHP Code:
$session->set('user''someone'); 
I was also facing same problem. I got my answer, thaks a lot
Hello Bro, i have joined this community to improve my skills not for spam
The error appears because you are using CI3, I gave an example for CI4. CI4 has been rewritten and therefore they are incompatible.
If you still have errors, then describe them here, but rather best to provide logs from the folder /writable/logs/ or /writable/debug/ if they are available.