CodeIgniter Basic Tutorial
Performance & Utilities
CodeIgniter Advanced
Flashdata in CodeIgniter is used to store temporary session data that is only available for the next server request (one page load).
It is commonly used for showing success, error, or warning messages after actions like form submission, login, or data updates.
For example:
Make sure the Session Library is loaded.
In application/config/autoload.php
:
$autoload['libraries'] = array('session');
Or manually in a controller:
$this->load->library('session');
You can set flashdata like this:
// Set flashdata $this->session->set_flashdata('success', 'Your account has been created successfully!');
// Get flashdata echo $this->session->flashdata('success');
Note
⚡ Flashdata will be cleared automatically after it is used once.
class User extends CI_Controller { public function register() { // After saving user $this->session->set_flashdata('success', 'User registered successfully!'); redirect('user/success'); } public function success() { $this->load->view('success_view'); } }
<?php if($this->session->flashdata('success')): ?> <p style="color: green;"> <?= $this->session->flashdata('success'); ?> </p> <?php endif; ?>
If you want flashdata to last beyond one request, use keep_flashdata()
:
$this->session->set_flashdata('warning', 'This is a warning!'); $this->session->keep_flashdata('warning'); // keeps it for another request