08.10.2017 Views

codeigniter_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15. Tempdata<br />

CodeIgniter<br />

In some situations, where you want to remove data stored in session after some specific<br />

time-period, this can be done using tempdata functionality in CodeIgniter.<br />

Add Tempdata<br />

To add data as tempdata, we have to use mark_as_tempdata() function. This<br />

function takes two argument items or items to be stored as tempdata and the<br />

expiration time for those items are as shown below.<br />

// 'item' will be erased after 300 seconds(5 minutes)<br />

$this->session->mark_as_temp('item',300);<br />

You can also pass an array to store multiple data. All the items stored below will be<br />

expired after 300 seconds.<br />

$this->session->mark_as_temp(array('item','item2'),300);<br />

You can also set different expiration time for each item as shown below.<br />

// 'item' will be erased after 300 seconds, while 'item2'<br />

// will do so after only 240 seconds<br />

$this->session->mark_as_temp(array(<br />

'item'=>300,<br />

'item2'=>240<br />

));<br />

Retrieve Tempdata<br />

We can retrieve the tempdata using tempdata() function. This function assures that<br />

you are getting only tempdata and not any other data. Look at the example given below<br />

to see how to retrieve tempdata. tempdata() function will take one argument of the<br />

item to be fetched.<br />

$this->session->tempdata('item');<br />

If you omit the argument, then you can retrieve all the existing tempdata.<br />

Remove Tempdata<br />

Tempdata is removed automatically after its expiration time but if you want to remove<br />

tempdata before that, then you can do as shown below using the unset_tempdata()<br />

function, which takes one argument of the item to be removed.<br />

$this->session->unset_tempdata('item');<br />

61

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!