• Main Menu
  • Display Messages using Flashdata in CodeIgniter

    Flashdata is a functionality in the CodeIgniter framework that lets you make required data available for the next server request. It makes use of session, but unlike normal session behavior, the passed data is available only for the next server request and is automatically cleared thereafter.

    Therefore, Flashdata can be very useful in displaying notifications and messages on the next page – like error messages, success messages, warning messages, and informational messages.

    The session library should already be loaded via config/autoload.php or manually in the controller.

    Let’s take an example for four types of messages via Flashdata – error, success, warning and informational.

    Step 1: Place the following code in a view file, like header.php which is mostly common throughout the app. An appropriate message will be shown depending on the session availability.

    <p class='flashMsg flashSuccess'> <?=$this->session->flashdata('flashSuccess')?> </p>
    <p class='flashMsg flashError'> <?=$this->session->flashdata('flashError')?> </p>
    <p class='flashMsg flashInfo'> <?=$this->session->flashdata('flashInfo')?> </p>
    <p class='flashMsg flashWarning'> <?=$this->session->flashdata('flashWarning')?> </p>

    Messages can be styled via CSS as per your requirements using .flashMsg, .flashSuccess, .flashError, .flashWarning and .flashInfo classes.

    For example, an error message can be colored red, success message can be colored green, warning message can be colored yellow and informational message can be colored blue – with suitable icons of course.

    Step 2: In order to pass the message data, you will need to set flashdata in a controller as shown below. A page redirection after setting of flashdata is necessary for it to work correctly.

    // Display success message
    $this->session->set_flashdata('flashSuccess', 'This is a success message.');


    // Display error message
    $this->session->set_flashdata('flashError', 'This is an error message.');


    // Display warning message
    $this->session->set_flashdata('flashWarning', 'This is a warning message.');


    // Display informational message
    $this->session->set_flashdata('flashInfo', 'This is an informational message.');

    The flashdata item names like ‘flashError’, ‘flashSuccess’, ‘flashWarning’ and ‘flashInfo’ should be used cautiously without making any spelling mistake.


    1. Using CodeIgniter session with database may cause trouble while using flashdata if the server environment is not stable. If your flashdata doesn’t work, I recommend setting $config[‘sess_use_database’] = FALSE; in the config.php file and retry.
    2. While redirecting a page it may happen that there are multiple redirections and hence flashdata could be lost. If this happens, use $this->session->keep_flashdata(‘item’); to preserve data through an additional request.
    3. Make sure the item names used are exactly the same while setting and accessing flashdata.

    Got Something To Say:

    Your email address will not be published. Required fields are marked *

    1. kaanjeepan

      18 October, 2014 at 11:32 am

      thank you very much…………

    2. umar

      14 April, 2014 at 10:20 am

      thank you

    3. renzo

      22 August, 2013 at 3:34 pm


    } 412 queries in 0.368 seconds.