Set and Read Flash Data in Codeigniter

Codeigniter Flash Data is a temporarily stored value.At the time of web application development, we need to store data only for one request. For example, Display success message, Error message and etc.

In CodeIgniter Flash Data is doing the same job for us.

Set flash data

$this->session->set_flashdata('item', 'value');

Read flash data



We will use following step to Set and Read Flash Data in CodeIgniter.

Step 1

Create Controller file inside "application/controllers/flashmsg.php".

defined('BASEPATH') OR exit('No direct script access allowed');
class flashmsg extends CI_Controller {
public function index() { 
         //Load session library 
         //redirect to home page 
      public function add() { 
         //Load session library 
         //add flash data 
         $this->session->set_flashdata('item','Hi Max.'); 
         //redirect to home page 

Step 2

Create view files inside "application\views\flashmsg.php".

<title>CodeIgniter Flashdata Example By</title> 
CodeIgniter Flash Data Example 
<h2><?php echo $this->session->flashdata('item'); ?></h2> 
<a href = '<?php echo base_url(); ?> flashmsg/add'> Click Here</a>  to add flash data. 
Step 3

Now we have to visit the following Url in our web browser.