CodeIgniter Q & A


How to handle and display flash messages in CodeIgniter?

Handling and displaying flash messages in CodeIgniter is a common practice for providing feedback and notifications to users after specific actions, such as form submissions or successful updates. Flash messages are typically shown to users on one page and then automatically cleared on the next page load. Here’s a step-by-step guide on how to implement flash messages in CodeIgniter:


  1. Load the Session Library:

   To use flash messages, you need to ensure that the CodeIgniter Session library is loaded. You can do this by adding the following line to your controller’s constructor or method where you plan to use flash messages:






  1. Set Flash Messages:

   To set a flash message, you can use the `set_flashdata` method of the session library. For example, to set a success message:



   $this->session->set_flashdata('success', 'Your operation was successful.');



   You can set flash messages for different scenarios, such as success, error, warning, or any custom message you need.


  1. Display Flash Messages:

   To display flash messages in your views, you can use the `flashdata` method within your view files. For instance, in your view file (usually a template or layout file), you can check for flash messages and display them like this:



   <?php if ($this->session->flashdata('success')): ?>

       <div class="alert alert-success">

           <?php echo $this->session->flashdata('success'); ?>


   <?php endif; ?>



   Repeat this pattern for each type of flash message you want to display (e.g., error, warning, etc.).


  1. Automatic Removal:

   CodeIgniter’s flash messages are automatically removed from the session after they have been displayed once. This ensures that messages are only shown to users on the subsequent page load and won’t persist across multiple requests.


By following these steps, you can effectively handle and display flash messages in your CodeIgniter applications. Flash messages are a useful tool for providing user feedback and enhancing the user experience by keeping users informed about the results of their actions.

Previously at
Flag Argentina
time icon
Experienced Full Stack Systems Analyst, Proficient in CodeIgniter with extensive 5+ years experience. Strong in SQL, Git, Agile.