I would like to store the variable in the session currently i am tryin like this but not working.
In Controller
<?php
session_start();
$_SESSION['myvar']='myvalue';
?>
Add Session Data The same thing can be done in CodeIgniter as shown below. $this->session->set_userdata('some_name', 'some_value'); set_userdata() function takes two arguments. The first argument, some_name, is the name of the session variable, under which, some_value will be stored.
The function userdata("index_to_data") is one of the main class methods. It is used to retrieve data that has been stored in the session class. The argument passed to userdata() is the key to a value in the session class array $userdata .
You have to load session library. Add a __construct method to the Login controller and load the library. This will let you access to the library's functions in any method inside the controller. I use to get the session data in the controller instead of the model.
To clear the current session (for example, during a logout), you may simply use either PHP's session_destroy() function, or the library's destroy() method.
You don't have to set the session the PHP way.
CodeIgnter has its on Session Class
Load the session library:
$this->load->library('session');
To set session data:
$this->session->set_userdata('some_name', 'some_value');
To retrieve session data:
$this->session->userdata('some_name');
To remove session data:
$this->session->unset_userdata('some_name');
Simple :
First, load this library
$this->load->library('session');
Then, to add some informations in session :
$newdata = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
Next, if you want to get values :
$session_id = $this->session->userdata('session_id');
And to remove :
$this->session->unset_userdata('some_name');
A simple search "codeigniter session" could have help you ...
https://www.codeigniter.com/user_guide/libraries/sessions.html
Don't forget to upvote and mark as solved if you find this useful :)
I consider storing my session data in a controller. So this is how you should go about storing and retrieving session data
1.Use autoload in your config.php files. It saves you time and load time
autoload['libraries'] = ('session);
2.In your controller.php create an array to store your session data.
$new_data = array( 'username' => 'martin', 'email' => '[email protected]', 'user_logged => TRUE );
$this->session->set_userdata($new_data);
$username = $this->session->userdata('username');
For further reference visit codeigniter user_guide/sessions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With