Hi i have made the base controller with the name of MY_Controller and have the following code
<?php
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true) {
redirect('login');
}
}
}
?>
my login.php controller is
class Login extends CI_Controller {
public function index()
{
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$this->load->model('users');
$record = $this->users->authenticate_user($email, $password);
if (count($record) && $record != false) {
foreach ($record as $user) {
$userdata = array(
'is_logged_in' => true
);
}
$this->session->set_userdata($userdata);
redirect('index');
}
else
{
$this->load->view('login');
}
}
}
and all other controllers are extended like this
class Index extends MY_Controller
{
function __construct(){
parent::__construct();
}
}
class Messenger extends MY_Controller
{
function __construct(){
parent::__construct();
}
}
but session is just available in index controller. even all other controller also extended with MY_Controller
AnyOne help meee
Where have you initialised your session library? Load your session library in autoload.php
and try.
$autoload['libraries'] = array('database', 'session'); //in autoload.php
In config.php:
$config['encryption_key'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //your key here
Put the session class into the autoloader configuration, in application/config/autoload.php
:
$autoload['libraries'] = array('session' /* , ... */);
Then it's available automatically in each controller:
$session_id = $this->session->userdata('session_id');
and you can initialize the session data after (and only after!) each login.