CodeIgniter Flashdata

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:

  • Show a “Login Successful” message after login.
  • Display “Data Saved Successfully” after saving data.

🔹 Step 1: Load Session Library

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');

🔹 Step 2: Set Flashdata

You can set flashdata like this:

// Set flashdata
$this->session->set_flashdata('success', 'Your account has been created successfully!');

🔹 Step 3: Retrieve Flashdata

// Get flashdata
echo $this->session->flashdata('success');

Note

⚡ Flashdata will be cleared automatically after it is used once.

🔹 Step 4: Example in Controller and View

Controller (User.php)

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');
    }
}

View (success_view.php)

<?php if($this->session->flashdata('success')): ?>
    <p style="color: green;">
        <?= $this->session->flashdata('success'); ?>
    </p>
<?php endif; ?>


🔹 Step 5: Keep Flashdata for Another Request

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



Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.