How to create 2 variable session in codeigniter?

Member

by jasen , in category: PHP CMS , 20 days ago

How to create 2 variable session in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jerad , 19 days ago

@jasen 

To create and use 2 variables in a session in CodeIgniter, you can follow these steps:

  1. Load the session library in your controller or autoload it in config/autoload.php:
1
$this->load->library('session');


  1. Set the session variables using the set_userdata() method:
1
2
$this->session->set_userdata('variable1', 'value1');
$this->session->set_userdata('variable2', 'value2');


  1. Retrieve the session variables using the userdata() method:
1
2
$variable1 = $this->session->userdata('variable1');
$variable2 = $this->session->userdata('variable2');


  1. Unset or destroy the session variables when they are no longer needed:
1
2
$this->session->unset_userdata('variable1');
$this->session->unset_userdata('variable2');


or

1
$this->session->sess_destroy();


Remember to also configure the session settings in config.php to make sure the session data is stored and managed properly.