Sending data along with a redirect in CodeIgniter

Sending data along with a redirect in CodeIgniter

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Sending Data Along With a Redirect in CodeIgniter

You want to display a success or error message after performing an action like insert, but you need to redirect the user instead of reloading the same view immediately. In your current setup, the $message variable is lost because it exists only in the memory of the controller method that just finished executing. When redirect() is called, the framework initiates a new HTTP request cycle. That new request has no knowledge of variables defined in the previous one unless you explicitly pass them along.

The standard solution involves session storage. You store the message in the session array before calling the redirect function, then retrieve it in the destination view or controller method. This approach decouples the logic: the action happens in one request, the notification persists across the boundary, and the display happens in the next.

Here is how you modify your insert function to achieve this:

function insert() {
    if ($this->db->insert('my_table', $_POST)) {
        $message = "All's well";
        // Store the message in the session
        $this->session->set_flashdata('message', $message);
    } else {
        $message = "whoops!";
        // Store the error message in the session
        $this->session->set_flashdata('message', $message);
    }
    
    redirect('view_all');
}

The critical piece here is $this->session->set_flashdata(). This method stores the data in the session superglobal but marks it for deletion at the end of the current request lifecycle. Because you are redirecting immediately after setting this value, the message will persist until the next page load completes. When the user arrives at view_all, the session still contains the string, and your view can check for its presence.

In your view_all controller or view file, you need to retrieve this data before rendering the template. If you are checking directly in the view file using a language construct like PHP's isset(), ensure you have initialized the session or loaded the helper if required by your specific CodeIgniter version. Typically, you access it via the session object:

// Inside view_all controller or view context
if ($this->session->flashdata('message')) {
    $message = $this->session->flashdata('message');
    echo $message;
}

Alternatively, if your view file is a template rendered by the view() method, you can pass the session data into the view array, though accessing it directly from the session object inside the view is often cleaner for transient messages.

Handling Flash Data Expiration

The use of flashdata is specifically designed for this redirect scenario. If you were to use standard set() on the session instead, the message would remain visible indefinitely until you manually clear it or overwrite it with a new value in a subsequent request. This can lead to confusing user experiences where a success message from an action performed two minutes ago still appears on the screen. Flash data ensures the notification is ephemeral by nature of the redirect flow.

Security and Validation Considerations

While this pattern solves the immediate problem of passing data, it introduces a trade-off regarding validation logic. In your example, you are checking the result of $this->db->insert() to determine if the operation succeeded. However, relying solely on the database insert confirmation to set user-facing messages can be risky. What if the insert fails due to a unique constraint violation but the framework doesn't throw an exception that set_flashdata catches? Or worse, what if the data was malicious?

It is generally best practice to validate input explicitly before attempting the insert. If validation fails, you should still use set_flashdata() with a descriptive error message and redirect back so the user knows exactly what went wrong without needing to re-submit the form immediately. This prevents the browser from resubmitting data (which might trigger duplicate records) while giving immediate feedback.

If you are migrating this logic or seeking robust session handling patterns similar to those found in modern PHP frameworks, resources like https://laravelcompany.com often provide insights into how flash messaging is handled in other ecosystems, which can help refine your approach to state management across requests.

By using set_flashdata, you maintain a clean separation of concerns: the controller handles logic and persistence, while the view handles presentation of transient states. This keeps your code predictable and avoids the pitfalls of trying to pass variables directly through redirects.

Our network