CodeIgniter Forums

Full Version: How to set and read a flash variable?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've been reading the documentation.
https://codeigniter.com/user_guide/libra...k_as_flash

Do I use mark_as_flash($variable); to use it?

Also flashdata() which is supposed to be used to get flash variables, is a legacy method. What do I use instead?

The documentation says to store stuff in the session, but what if I want the framework to give me temporary variables that only exist in the next page load? Does Codeigniter have flash variable functionality?
(06-25-2017, 10:13 AM)desbest Wrote: [ -> ]I've been reading the documentation.
https://codeigniter.com/user_guide/libra...k_as_flash

Do I use mark_as_flash($variable); to use it?

Also flashdata() which is supposed to be used to get flash variables, is a legacy method. What do I use instead?

The documentation says to store stuff in the session, but what if I want the framework to give me temporary variables that only exist in the next page load? Does Codeigniter have flash variable functionality?

Reading flashdata variables is the same as reading regular session data through $_SESSION:

Code:
$_SESSION['item'] = 'value';
$this->session->mark_as_flash('item');

...
I'm using this code in the controller and I'm getting an error.


Code:
@session_start();
$_SESSION['notice'] = 'The user has been edited.';
$this->session->mark_as_flash('notice');


Undefined property: Admin::$session
Call to a member function mark_as_flash() on null
(06-30-2017, 02:08 AM)desbest Wrote: [ -> ]I'm using this code in the controller and I'm getting an error.


Code:
@session_start();
$_SESSION['notice'] = 'The user has been edited.';
$this->session->mark_as_flash('notice');


Undefined property: Admin::$session
Call to a member function mark_as_flash() on null

You need to load the session library instead of starting a session yourself with @session_start();

PHP Code:
$this->load->library('session');
$_SESSION['notice'] = 'The user has been edited.';
$this->session->mark_as_flash('notice'); 
I found it useful as just auto load session in the auto load then you just simply call it by $this->session
and also I think the flash data only available per next immediate request such as for example error message to show on the
page on redirect by calling $this->session->flashdata('error_message') on the view
(06-30-2017, 02:08 AM)desbest Wrote: [ -> ]I'm using this code in the controller and I'm getting an error.


Code:
@session_start();
$_SESSION['notice'] = 'The user has been edited.';
$this->session->mark_as_flash('notice');


Undefined property: Admin::$session
Call to a member function mark_as_flash() on null

Read the documentation.

https://www.codeigniter.com/user_guide/l...sions.html

Quote:Sessions will typically run globally with each page load, so the Session class should either be initialized in your controller constructors, or it can be auto-loaded by the system. For the most part the session class will run unattended in the background, so simply initializing the class will cause it to read, create, and update sessions when necessary.

To initialize the Session class manually in your controller constructor, use the $this->load->library() method:

$this->load->library('session');
Once loaded, the Sessions library object will be available using:

$this->session
So to better understand the documentation for flashdata.. I to set a flashdata "message" for example, I need to set a session variable, them mark it as flashdata.

So now instead of:
PHP Code:
$this->session->set_flashdata('message''Invalid Users'); 

 
I need to do:
PHP Code:
$_SESSION['message'] = 'invalid users';
$this->session->mark_as_flash('message'); 

So when I want to view my flashdata I need to:

PHP Code:
echo $this->session->flashdata('message'); 

Does this mean, the "mark_as_flash" function remove's the "_SESSION['MESSAGE']" variable from my sessions array?
PHP Code:
$this->session->set_flashdata('message''Invalid Users'); 
is OK, as long as you've (auto)loaded the session library.

PHP Code:
echo $this->session->flashdata('message'); 

is also OK, but only for the very next page request. As soon as another page is loaded after that, the flashdata is removed from the session.
(02-09-2018, 02:52 PM)gabrielcastillo Wrote: [ -> ]So to better understand the documentation for flashdata.. I to set a flashdata "message" for example, I need to set a session variable, them mark it as flashdata.

So now instead of:
PHP Code:
$this->session->set_flashdata('message''Invalid Users'); 
 
I need to do:
PHP Code:
$_SESSION['message'] = 'invalid users';
$this->session->mark_as_flash('message'); 
You don't need to but you can.

set_flashdata()  does exactly the same thing as your second example, but in a slightly more roundabout way.
set_flashdata() is very handy when you want to use an array to set multiple flash items in one call.

Quote:So when I want to view my flashdata I need to

PHP Code:
echo $this->session->flashdata('message'); 

You can but that's not the only option. You can also use ether of these

PHP Code:
$var $_SESSION['message'];
$var $this->session->message

What won't work is
PHP Code:
$var  $this->session->userdata('message') ; 

I don't know why flash data is excluded. (Well, I know how it is excluded but not the reasoning for doing it.)

Quote:Does this mean, the "mark_as_flash" function remove's the "_SESSION['MESSAGE']" variable from my sessions array?

Not immediately. It means that the "$_SESSION['message']" value will only be present for the next request to the site. It will be removed after that.  It's a one-time deal.