How To Set & Unset Session In CodeIgniter

How To Set & Unset Session In CodeIgniter

Session set and unset are CodeIgniter’s most valuable features, allowing developers to store and retrieve user data across multiple page requests.

In this tutorial, you will explore how to set and unset sessions in CodeIgniter 4, 3, starting with an overview of what sessions are and why they are important.

Sessions are useful for a variety of purposes, such as:

  • Keeping users logged in
  • Storing user preferences
  • Tracking user activity
  • Storing shopping cart information
  • Personalizing content for users

How To Set & Unset Session In CodeIgniter

Now, let’s dive into how to set and unset sessions in CodeIgniter 4, 3.

  • Setting a session in CodeIgniter
  • Unsetting a session in CodeIgniter

Setting a session in CodeIgniter

To set a session in CodeIgniter, you first need to load the session library. This can be done by adding the following code to controller:

$this->load->library('session');

Once you have loaded the session library, you can set a session variable by calling the set_userdata() method.

Here’s an example to set session:

$this->session->set_userdata('username', 'john.doe');

In this example, you are setting a session variable named “username” with the value “john.doe”.

Here’s an example of how to set a session in an array:

$data = array(
'username' => 'john.doe',
'email' => '[email protected]',
'is_logged_in' => TRUE
);

$this->session->set_userdata($data);

In this example, you have defined an associative array named $data, which contains three key-value pairs representing the user’s username, email, and login status.

Unsetting a session in CodeIgniter

To unset a session variable in CodeIgniter, you can call the unset_userdata() method, passing in the name of the variable you want to unset as its argument.

Here’s an example of unset session:

$this->session->unset_userdata('username');

To unset a session variable that is stored in an array, you can use the unset() function to remove the key-value pair from the session array.

Here’s an example of how to unset a session variable that is stored in an array in CodeIgniter:

// Load the session library
$this->load->library('session');

// Initialize the session data as an array
$session_data = array(
'username' => 'john.doe',
'email' => '[email protected]',
'role' => 'admin'
);

// Set the session data
$this->session->set_userdata($session_data);

// Unset the 'email' session variable
unset($_SESSION['email']);

// Alternatively, you can also use the unset_userdata() method provided by the session library
$this->session->unset_userdata('email');

Conclusion

In this article, you have learned how to set and unset sessions in CodeIgniter, using the set_userdata() and unset_userdata() methods, respectively.

Recommended CodeIgniter Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *