In this tutorial we will learn about CodeIgniter Tempdata. In some situations, where you want to delete data stored in a session after a certain time period, the tempdata functionality in CodeIgniter can be used.

Add CodeIgniter Tempdata

In order to add data as tempdata, mark as tempdata() must be used. This function takes two items or items to be stored as tempdata and the time for these items is shown below.

// 'item' will be removed after 300 seconds(5 minutes)

You can pass an array to save multiple data as well. After 300 seconds, all items stored below expire.


As shown below, you can also set different expiry times for each item.

// 'item' will be removed after 300 seconds, while 'item2' 


// will do so after only 240 seconds

Retrieve CodeIgniter Tempdata

Use tempdata () function to retrieve the tempdata. This function ensures you only receive tempdata and not other data. Look at the example below to see how tempdata can be collected. The tempdata () function takes a fetched item argument.


If you omit the argument, all existing tempdata can be recovered.

Remove CodeIgniter Tempdata

Tempdata is automatically deleted after its expiration time, but if you want to delete tempdata before it, you can use the unset tempdata() function as shown below, which takes one argument from the item to be deleted.