Himanshu SharmaSoftware engineer
Let’s talk

CodeIgniter 4 · Lesson 18 of 30

CodeIgniter sessions: carry a message across a redirect

Carry an escaped success notice through a redirect using flashdata and distinguish temporary feedback from identity and durable records.

15 minForms and browser stateUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonCarry an escaped success notice through a redirect using flashdata and distinguish temporary feedback from identity and durable records.

After this lesson you can display a one-request success notice after a redirect and explain how flashdata differs from ordinary session state. HTTP requests do not automatically share local PHP variables. Sessions provide a mechanism for associating server-side state with a client across requests.

Follow the redirect as two requests

The create action finishes with a redirect and a notice. The browser then makes a new request to the catalogue. A variable local to create would not be available in index. Flashdata carries the intended message across that boundary without turning it into a permanent part of every catalogue page.

The success return already used in create is:

php
return redirect()->to('/books')->with('notice', 'Book added.');

This fragment was executed in the temporary application. It associates a flash value with the redirect flow. The word notice is our application key, not a reserved database column. Choose clear keys and avoid collisions when several features use session state.

Add the following block inside the content section of app/Views/books/index.php, before the list. The rest of the catalogue template stays as written previously.

php
<?php if ($notice = session()->getFlashdata('notice')): ?>
<p role="status"><?= esc($notice) ?></p>
<?php endif ?>

The template was linted and the message appeared after a real accepted form submission. It is escaped even though the current string is authored by the application. Keeping the display boundary consistent prevents a later change to message content from creating a hidden unsafe rendering path.

Flashdata has a lifecycle

CodeIgniter's Session Library documentation describes flashdata as session data intended for the next request and then cleared according to its lifecycle. It is not a reliable permanent record of an operation. If a message must survive additional requests, the framework has explicit mechanisms, but use them deliberately rather than converting every temporary notice into ordinary session data.

Reading a single flash key is different from asking for every ordinary session value. Do not debug sessions by dumping everything into a public page. Authentication-related state, preferences or other private values can share the same mechanism. Narrow inspection is both clearer and safer.

A success notice is not evidence that a later request will still find the inserted record. Another action might delete it, or an operational failure might occur after the original write. For durable audit requirements, use a designed audit trail rather than transient browser feedback.

Ordinary session state serves another purpose

A session can also store longer-lived state, such as an interface preference. That does not mean arbitrary request data should be copied into the session. Decide which values are needed, how long they should last and whether they are sensitive. Large data belongs in appropriate storage rather than being accumulated in session state without limits.

A session flag named logged_in is not a complete authentication system. Creating that flag from a form field would let a caller claim their own identity. Authentication requires a trustworthy verification process, and authorization must still decide what an authenticated identity may do. The Shield lesson discusses the boundary without inventing a hand-built login system.

A browser usually sends an identifier that lets the application locate its session state. Cookies need deployment-specific security settings, including appropriate HTTPS handling. Session storage needs suitable permissions and cleanup. The correct configuration depends on the host and whether multiple application instances share requests.

The local bookshop uses the starter session facilities only for fictional notices. It does not store real customer information or credentials. Before exposing a session-authenticated application, review the current official session and authentication guidance for your installed versions rather than treating this notice example as a complete session security configuration.

The fetched official Session Library guide documents flashdata and session methods. Our local HTTP check confirmed the accepted submission's notice on the redirected catalogue. A rejected tokenless request did not show the accepted-write notice, even though following a rejection redirect could end on a normal page.

Common mistakes

Expecting a local controller variable to survive a redirect misunderstands request boundaries. Storing every success message as ordinary session data makes stale notices persist. Treating a session flag as proof of authentication trusts the wrong source. Finally, dumping the complete session to diagnose a missing notice can expose unrelated private values.

Exercise

Explain why the notice must cross a request boundary and where it is escaped. Then decide whether flashdata is an appropriate place for a permanent record of who edited a book and justify your answer.

Show an answer

The redirect causes a new catalogue request, so the create method's local variables are gone. Flashdata carries the notice and the list template escapes it as HTML text. It is unsuitable for a durable audit trail because it is temporary client-associated feedback rather than a permanent, reviewed record of an authenticated action.

Previous lesson · Next lesson · Related: explicit routes · Related: security boundaries

Key points

  • A redirect starts another request.
  • Flashdata is temporary session-backed feedback.
  • Session values alone do not establish trustworthy authentication.

Check yourself

1. Why not use a local create variable for the notice?
2. Where is the notice escaped?
3. Is flashdata a durable audit trail?