Where to set session in Codeigniter?
Based on the docs, to do anything custom within session you need to load the session library. If you plan to use session throughout your application, I would recommend autoloading the library. You do this from within config/autoload.php.
$autoload['libraries'] = array('session');
Then you won't have to use $this->load->library('session');
on every page.
After the library is loaded, set your custom information, maybe based off some information from your database. So in your case, this would be in login.php:
$this->session->set_userdata('userId', 'myId');
where userId
would be the name of the session variable, and myId
would be the value.
Then, on subsequent pages (admin.php), you could check that the value is there.
if($this->session->userdata('userId') == '') { //take them back to signin }
To set user session
$the_session = array("key1" => "value1", "key2" => "value2");
$this -> session -> set_userdata($the_session);
To read user session
$foo = $this -> session -> userdata('key1');
You need $this->load->library('session');
every time prior you use CI session functions. Or you can set it up in autoload.php $autoload['libraries'] = array('session');
load session library
$this->load->library('session');
-
$_SESSION['email'] = $data['email'];
-
$this->session->unset_userdata($_SESSION['email']); // $this->session->sess_destroy();