Codeigniter 4 – Session library with Example
Configuring the Session Library
- Open the app/Config/Session.php file.
- Set the driver to the session driver you want to use (e.g. CodeIgniter\Session\Handlers\FileHandler).
- Set the sessionName to the name of the session cookie.
Example:
public $driver = 'CodeIgniter\Session\Handlers\FileHandler';
public $sessionName = 'ci_session';
Starting a Session
- Use the session() method to start a session.
Example:
session();
Setting Session Data
- Use the set() method to set session data.
Example:
session()->set('username', 'john_doe');
Getting Session Data
- Use the get() method to get session data.
Example:
$username = session()->get('username');
Removing Session Data
- Use the remove() method to remove session data.
Example:
session()->remove('username');
Destroying a Session
- Use the destroy() method to destroy a session.
Example:
session()->destroy();
Using Flashdata
- Use the setFlashdata() method to set flashdata.
Example:
session()->setFlashdata('message', 'Hello, World!');
Getting Flashdata
- Use the getFlashdata() method to get flashdata.
Example:
$message = session()->getFlashdata('message');
I hope this helps! Let me know if you have any further questions or need more information.
Note: The Session Library in CodeIgniter 4 provides a simple and intuitive way to manage session data. It helps to improve the security and reliability of your application.