- Microsoft 365 Outage: Outlook, Teams and Copilot Down. What We Know
- Diwali Online Shopping Scams 2026: 10 Ways to Stay Safe
- 10 Best DDoS Protection Services for 2027
- 12 Best Web Application Firewalls (WAF) for 2027
- Best Ethical Hacking Tools in 2027: Free & Paid Picks
- Software supply chain attacks 2026: Timeline and developer checklist
- Binance Hack: $6.3 Billion Drained in the Largest Crypto Theft Ever. What We Know So Far
- This Site Uses XSLT Chrome Warning Explained: How to Fix It
The Session class allows you to maintain a user’s “state” and track their activity while they browse your site.
Session Initialization
To access and initialize the session:
$session = Config\Services::session($config);
Example:
$this->session = Config\Services::session();
Add data to the session
A set() method is used to add value to the session. It takes two arguments as a parameter first is session name and the second is session value.
$this->session->set('session_name', 'any_value');We can also pass an array to the set() method to store values in session.
$data = array( 'username' => 'infovistar', 'email' => 'info@infovistar.com', 'logged_in' => TRUE ); $this->session->set($data);
Fetch data from Session
A get() method is used to get data from the session. It takes the session key name as an argument. For example:
$name = $this->session->get('username');Remove data from Session
A remove() method is used to remove data from the session. It takes the session key name as an argument. For example:
$this->session->remove('session_name');If you want to remove more than one value then you can use the same remove() function.
$data = array('username', 'email');
$this->session->remove($data);Destroying a Session
To clear the current session (for example, during a logout), you may simply use either PHP’s session_destroy() function or the library’s destroy() method. Both will work in the same way:
session_destroy();
// or
$this->session->destroy();
Flashdata
While building a web application, we need to store some data for only one time, and later, we want to remove that data. For example, to display some error message or information message. In CodeIgniter, flashdata will only be available until the next request, and it will get deleted automatically.
Add Flashdata:
$this->session->setFlashdata('item','value');You can also pass an array to setFlashdata(), in the same manner as set().
$data = array(
'username' => 'infovistar',
'email' => 'info@infovistar.com'
);
$this->session->getFlashdata($data);
Retrieve Flashdata:
$this->session->getFlashdata('item');Or to get an array with all flashdata, simply omit the key parameter:
$this->session->getFlashdata();



