Skip to content

docs: Notes about switching types in session #9508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions user_guide_src/source/libraries/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ intend to reuse that same key in the same request, you'd want to use

.. literalinclude:: sessions/036.php

Changing the Session Key Type
=============================

Since session data values like Flashdata and Tempdata are differentiated only by internal flags, you can change a value's type without rewriting its data.

.. literalinclude:: sessions/045.php

Closing a Session
=================

Expand Down
25 changes: 25 additions & 0 deletions user_guide_src/source/libraries/sessions/045.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

session()->setFlashdata('alerts', 'Operation successful!');

/*
* Get flash value 'Operation successful!' in another controller.
*
* echo session()->getFlashdata('alerts');
*/

// You can switch the session key type from Flashdata to Tempdata like this:
session()->markAsTempdata('alerts');

// Or simply rewrite it directly
session()->setTempdata('alerts', 'Operation successful!');

/*
* Get temp value 'Operation successful!' in another controller.
*
* echo session()->getTempdata('alerts');
*
* But flash value will be empty 'null'.
*
* echo session()->getFlashdata('alerts');
*/