Tempdata in CodeIgniter
0 2585
- 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. Syntax:$this->session->mark_as_flash('item',200);
Here,- the item represents the name of the variable which we want to make tempdata.
- the second parameter is used to set the expiration time.
$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'2 By using set_tempdata() method: Syntax:
// will do so after only 300 seconds
$this->session->mark_as_temp(array(
'item1'=>200,
'item2'=>300
));
$this->session->set_tempdata('item', 'value',200);
Where,- 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. Syntax:$this->session->tempdata('item');
Where,- the item represents the name of the variable which we want to fetch by the tempdata() method.
Note:
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. Syntax:$this->session->unset_tempdata('item');
Where,- the item represents the name of the variable which we want to unset.
Example: Step 1 Open directory application/views and create a view file tempdata_view.php.
<!DOCTYPE html>Step 2 Open directory application/controllers and create a controller file Tempdata_controller.php.
<html>
<head>
<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>
</head>
<body>
<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.
</div>
</body>
</html>
<?phpStep 3 Open the given URL into the browser to see the result.
class Tempdata_controller extends CI_Controller {
public function index() {
//Load session library
$this->load->library('session');
//redirect to home page
$this->load->view('tempdata_view');
}
public function add() {
//Load session library
$this->load->library('session');
$this->load->helper('url');
//add flash data
$this->session->set_tempdata('website','www.codingtag.com',300);
//redirect to home page
redirect('http://localhost/ci/index.php/Tempdata_controller');
}
}
?>
http://localhost/ci/index.php/Tempdata_controller
Click on the "click here" link to add tempdata.

Share:




Comments
Waiting for your comments