Welcome Guest, Not a member yet? Register   Sign In
Setting and Getting Session and Flashdata
#1

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'); 
Reply
#2

$session = session();
$session->set('user', 'someone');
Reply
#3

(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?
Reply
#4

(This post was last modified: 09-26-2019, 02:20 AM by Digital_Wolf. Edit Reason: added link )

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
I would change this world, but God doesn't give me the source.
Reply
#5

(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

Reply
#6

(This post was last modified: 09-26-2019, 02:55 AM by sktiwari19.)

(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
Reply
#7

(This post was last modified: 09-26-2019, 03:42 AM by titounnes.)

CI4, different with CI3.
learn about filter.
https://github.com/codeigniter4/CodeIgni...ilters.rst

use BaseConttoller for autoload libraries, helper etc.
Reply
#8

(This post was last modified: 09-26-2019, 04:47 AM by msjagan.)

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.
Reply
#9

(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
Reply
#10

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.
I would change this world, but God doesn't give me the source.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB