Tempdata in CodeIgniter

Tempdata in CodeIgniter


  • Sometimes we want to use the value of a variable for a specific time period and after the use, we want to delete it.
  • Codeigniter provides tempdata concepts for it.
  • tempdata is session data that will be available for a specific time span means it has a specific expiration time and after the value expires, or the session expires or is deleted, it will be cleared automatically.

Related Topics:

Codeigniter Interview Questions
Flashdata in Codeigniter
Session management in Codeigniter

Add Tempdata:

We have two methods to add tempdata.

By using mark_as_temp() method: This function is used to mark an existing variable or array as tempdata.




  • the item represents the name of the variable which we want to make tempdata.
  • the second parameter is used to set the expiration time.

In the case of the array the syntax will be:

$this->session->mark_as_flash(array('item1', 'item2'),200);

Where item1 and item2 are the keys of an existing array.

We can also set a different expiration time for each item of the array.

// 'item1' will be erased after 200 seconds, while 'item2'
// will do so after only 300 seconds


2 By using set_tempdata() method:


$this->session->set_tempdata('item', 'value',200);


  • Item is the name of session variable and
  • value is the value of the variable item.
  • The third parameter is used to set the expiration time

Retrieving Tempdata:

We can retrieve the tempdata with the help of the tempdata() method.




  • the item represents the name of the variable which we want to fetch by the tempdata() method.

Although tempdata is session data, we cannot retrieve them by using the userdata() method.

Removing Tempdata:

Tempdata is deleted automatically at its expiration time. To remove it before its expiration time, we will use unset_tempdata() method.




  • the item represents the name of the variable which we want to unset.


Step 1 Open directory application/views and create a view file tempdata_view.php.

<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

<div class="container">
<h2>Tempdata example</h2>

<h4><?php echo $this->session->tempdata('website'); ?></h4>
<a href = 'Tempdata_controller/add'>Click Here</a> to add Temp data.



Step 2 Open directory application/controllers and create a controller file Tempdata_controller.php.

class Tempdata_controller extends CI_Controller {

public function index() {
//Load session library

//redirect to home page

public function add() {
//Load session library

//add flash data 

//redirect to home page

Step 3 Open the given URL into the browser to see the result.


Click on the "click here" link to add tempdata.

Best WordPress Hosting


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price

Leave a Reply

    Waiting for your comments