Online Snippets Library

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

$this->session->flashdata('item');

Example

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

Step 1

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

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

?>
Step 2

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

<html>
<head> 
<title>CodeIgniter Flashdata Example By Tutsway.com</title> 
</head>
<body>
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. 
</body> 
</html>
Step 3

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

http://localhost/ci/flashmsg/