CodeIgniter-FlashData

When creating a web application, we need to store some data only once, and after that we want to delete this data. For example, to display some error message or informational message. In PHP, we have to do this manually, but CodeIgniter has made it easy for us. In CodeIgniter, flashdata will only be available until the next request and will be automatically deleted.
Add flashdata
We can just store flash data like below.
$this->session->mark_as_flash('item');
- This is done using the mark_as_flash() function , which takes only one argument of the value to be stored. We can also pass an array to hold multiple values.
- You can also use the set_flashdata() function , which takes two arguments, a name and a value, as shown below. We can also pass in an array.
This is done using the mark_as_flash() function , which takes only one argument of the value to be stored. We can also pass an array to hold multiple values.
You can also use the set_flashdata() function , which takes two arguments, a name and a value, as shown below. We can also pass in an array.
$this->session->set_flashdata('item','value');
Get Flashdata
Flashdata can be retrieved using the flashdata() function which takes a single element argument to retrieve as shown below. The flashdata() function ensures that only flash data is received and no other data is received.
$this->session->flashdata('item');
If you don't pass an argument, you can get an array with the same function.
example
Create a class named FlashData_Controller.php and save it in application/controller/FlashData_Controller.php .
Create a view file called flashdata_home.php and save it to application/views/flashdata_home.php
<metacharset="utf-8">
Flash Data Example
Click Here to add flash data.
Make changes to the rout.php file in application/config/rout.php and add the following line to the end of the file.
$route['flashdata'] = 'FlashData_Controller';
$route['flashdata/add'] = 'FlashData_Controller/add';
Run the above example by visiting the following link. Replace yoursite.com with your site's URL.
http://yoursite.com/index.php/flashdata
After visiting the above URL, you will see the screen shown below.
Click on the " Click here " link and you will see a screen as shown below. Here on this screen you will see the value of the flash data variable. Refresh the page again and you will see a screen like the one above and the flash data variable will be automatically removed.
CodeIgniter - Tempdata
In some situations where you want to delete data saved in a session after a certain period of time, this can be done using the temporary data feature in CodeIgniter.
Add Tempdata
To add data as temporary data, we must use the mark_as_tempdata() function . This function takes two argument elements or elements to be stored as temporary data and the expiration of these elements is shown below.
// 'item' will be erased after 300 seconds(5 minutes)
$this->session->mark_as_temp('item',300);
You can also pass an array to store multiple data. All items listed below will expire after 300 seconds.
$this->session->mark_as_temp(array('item','item2'),300);
You can also set a different expiration time for each item as shown below.
// 'item' will be erased after 300 seconds, while 'item2'
// will do so after only 240 seconds
$this->session->mark_as_temp(array(
'item'=>300,
'item2'=>240
));
Get Tempdata
We can get temporary data using the tempdata() function . This function ensures that you only get temporary data and not any other data. Look at the example below to see how to get temporary data. The tempdata() function will take one element argument to fetch.
$this->session->tempdata('item');
If you omit an argument, you can get all existing temporary data.
Delete TempData
Tempdata is deleted automatically after it expires, but if you want to delete tempdata before then, you can do it like below using the unset_tempdata() function , which takes a single element argument to be deleted.
$this->session->unset_tempdata('item');
example
Create a class named Tempdata_controller.php and save it to application/controller/Tempdata_controller.php .
Create a file called tempdata_view.php and save it to application/views/tempdata_view.php
<metacharset="utf-8">
Temp Data Example
Click Here to add temp data.
Make changes to the rout.php file in application/config/rout.php and add the following line to the end of the file.
$route['tempdata'] = "Tempdata_controller";
$route['tempdata/add'] = "Tempdata_controller/add";
Run the above example by visiting the following link. Replace yoursite.com with your site's URL.
http://yoursite.com/index.php/tempdata
After visiting the above URL, you will see the screen shown below.
Click on the "Click here" link and you will see a screen as shown below.
Here on this screen you will see the value of the temp data variable. Refresh the same page after five seconds again as we have set the temporary data to five seconds and you will see a screen like the one above and the temporary data variable will be automatically deleted after five seconds. If you refresh the same page earlier than 5 seconds later, the temporary data will not be deleted because the time period has not expired.
Session destruction
In PHP we use the session_destroy() function to destroy the session and in CodeIgniter we can destroy the function as shown below.
$this->session->sess_destroy();
After calling this function, all session data, including flashdata and tempdata, will be permanently deleted and cannot be restored back.