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]
unset_tempdata()
function has to be used.
[php]$this->session->unset_tempdata('item');[/php] 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.