How To Set & Unset Session In CodeIgniter

How To Set & Unset Session In CodeIgniter

Session set and unset is the most valuable features of CodeIgniter in its session management method, which allows 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.

A session is a way for a web application to store user data across multiple page requests. When a user visits a website, a unique session ID is generated, which is then used to track the user’s activity on the site. The session ID is typically stored in a cookie on the user’s device.

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
    • Set session in variable
    • Set session in array
  • Unsetting a session in CodeIgniter
    • unset session in variable
    • unset session in array

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 our controller:

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

Once you have loaded the session library, you can set a session variable by calling the set_userdata() method. This method takes an associative array as its argument, where the keys represent the session variable names and the values represent their corresponding values.

Set session in variable

Here’s an example:

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

In this example, you are setting a session variable named “username” with the value “john.doe”. This value will be stored in the session and can be accessed on subsequent page requests.

Set session in array

Setting a session in an array in CodeIgniter is similar to setting a regular session variable, with the difference being that you need to pass an associative array to the set_userdata() method. Here’s an example of how to set a session in an array in CodeIgniter:

$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. Then pass this array as an argument to the set_userdata() method, which sets each key-value pair as a separate session variable.

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:

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

unset session in variable

In this example, you are unsetting the “username” session variable.

It’s worth noting that calling unset_userdata() without passing in any arguments will unset all session variables.

unset session in array

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');

In this example, you first load the session library and initialize the session data as an array. you then set the session data using the set_userdata() method. To unset the ’email’ session variable, you use the unset() function to remove the ’email’ key-value pair from the session array. Alternatively, you can also use the unset_userdata() method provided by the session library to unset the session variable.

It’s important to note that when unsetting a session variable that is stored in an array, only the key-value pair for that particular session variable is removed from the session array. The other session variables in the array remain unaffected.

Conclusion

Sessions are an important part of web development, allowing developers to store and retrieve user data across multiple page requests. CodeIgniter makes it easy to work with sessions, providing a simple and intuitive session management system.

In this article, you have learned how to set and unset sessions in CodeIgniter, using the set_userdata() and unset_userdata() methods, respectively. With this knowledge, you should be well equipped to start working with sessions in your CodeIgniter projects.

Recommended CodeIgniter Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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