Codeigniter - SPLessons

CodeIgniter Tempdata

Home > Lesson > Chapter 26
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Tempdata

CodeIgniter Tempdata

shape Description

CodeIgniter Tempdata is a Session related that stores the user information depending on time sequences. Once the given time is finished, the data values in Tempdata are deleted automatically.

Adding Tempdata

shape Description

To use Tempdata in CodeIgniter application, mark_as_tempdata() function have to be included which incorporates two parameters to store the values and time. Below is the syntax for CodeIgniter Tempdata: [php] // 'item' will be deleted after 60 seconds(2 minutes) $this->session->mark_as_temp('item_name',60); [/php] Not only single value, multiple values can be added to the Tempdata of the application using array. [php]$this->session->mark_as_temp(array('item','item2'),60);[/php] Different expiration time can also be fixed for the array of values. [php] // '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 )); [/php]

Retrieving Tempdata

shape Description

The data in Tempdata can be retrieved using the function tempdata() which confirms that only tempdata is only retrieved and not any other. The tempdata() has only single argument representing the item to be fetched. [php]$this->session->tempdata('item');[/php] If not specified the argument, then all the data present in tempdata are fetched.

Removing Tempdata

Normally, the data in Tempdata will be erased immediately after the given expiration time but if needed to remove the data manually, the function unset_tempdata() function has to be used. [php]$this->session->unset_tempdata('item');[/php]

shape Example

Create the controller example_controller.php in the file path application/controllers and enter the below code. [php] <?php class Example_controller extends CI_Controller { public function index() { $this->load->library('session'); $this->load->view('view_tempdata'); } public function add() { $this->load->library('session'); $this->load->helper('url'); //tempdata will be removed after 5 seconds $this->session->set_tempdata('item','splessons',5); redirect('tempdata'); } } ?> [/php] Then create the view to display the output. [php] <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>CodeIgniter Tempdata Example</title> </head> <body> Temp Data Example <h2><?php echo $this->session->tempdata('item'); ?></h2> <a href = 'example/add'>Click Here</a> to add temp data. </body> </html> [/php] Enter the below url(domain name varies) and check the output. [html]http://localhost/Codeigniter/index.php/example_controller[/html] Output: When clicked on Click Here button, the output with the tempdata appears. As the time duration given is 5 seconds, once that time is completed, and the page is refreshed, first output appears instead of second output.

Summary

shape Key Points

  • CodeIgniter Tempdata is a Session whose data values are deleted automatically when the given time expires.
  • CodeIgniter Tempdata - The function mark_as_tempdata() adds the values to Tempdata.