@raphael_tillman
In CodeIgniter, sessions can be controlled using the session library provided by the framework. Here are some steps to control sessions in CodeIgniter:
- Load the session library: First, you need to load the session library in your controller or autoload it so that you can access the session data.
1
|
$this->load->library('session');
|
- Start the session: Before you can use the session, you need to start it in your controller. This can be done in the constructor or in a specific method where you want to start the session.
1
|
$this->session->start();
|
- Set session data: You can set session data using the set_userdata() method. This method takes an associative array with the data you want to store in the session.
1
|
$this->session->set_userdata('user_id', 123);
|
- Get session data: You can retrieve session data using the userdata() method. This method takes the key of the data you want to retrieve.
1
|
$user_id = $this->session->userdata('user_id');
|
- Remove session data: You can remove session data using the unset_userdata() method. This method takes the key of the data you want to remove.
1
|
$this->session->unset_userdata('user_id');
|
- Destroy the session: You can destroy the session completely using the sess_destroy() method. This will remove all session data and destroy the session cookie.
1
|
$this->session->sess_destroy();
|
By following these steps, you can control sessions in CodeIgniter effectively and securely.