CodeIgniter Basic Tutorial
Performance & Utilities
CodeIgniter Advanced
Tempdata in CodeIgniter is similar to Flashdata, but with one big difference:
This makes Tempdata very useful when you want temporary session data that lasts for multiple requests, but not permanently.
Example use cases:
First, make sure the session library is loaded.
In application/config/autoload.php
:
$autoload['libraries'] = array('session');
Or load it in the controller:
$this->load->library('session');
You can set tempdata with a custom expiry time (in seconds).
// Set tempdata with 5 minutes expiry (300 seconds) $this->session->set_tempdata('message', 'This will last for 5 minutes.', 300);
To fetch tempdata:
echo $this->session->tempdata('message');
class Welcome extends CI_Controller { public function index() { // Set tempdata with 10 seconds expiry $this->session->set_tempdata('greeting', 'Hello! This message will expire in 10 seconds.', 10); $this->load->view('welcome_message'); } }
<?php if ($this->session->tempdata('greeting')): ?> <p style="color: blue;"> <?= $this->session->tempdata('greeting'); ?> </p> <?php endif; ?>