I have created a MY_Controller class to check for sessions.
My LoginController is checking the user information and if that is ok, I redirect the user to the PainelController. I use redirect so my url will be refresh with /localhost/painel instead of /localhost/session/login
The problem is that when I use redirect, I cannot access my session, only using load->view.
Is there a workaround?
Thanks in advance for any help.
PS: I use a .htaccess, the one found on CI Wiki
EDIT
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if(!$this->session->userdata('usuario')) {
redirect('login');
}
}
}
Piece of Login: extends CI_Controller
if( $rs )
{
$this->session->set_userdata('usuario', $usuario);//usuario is a object
//$this->load->view('painel');//it works
redirect('painel', 'location');//it doesn't
}
else
{
$this->load->view('login', $data = array('mensagem'=>'Usuário ou senha inválidos.'));
}
—
My Painel View
echo $this->session->userdata('usuario')->usuario_nome; //it works
only if I load->view('painel')
Even if I try to access this session value on my PainelController (extends MY_Controller) it won’t work, will say:
Message: main() [function.main]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Usuario" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition
I tried redirecting a couple of times on my CI sandbox but I didn’t lose my session.
What libraries/helpers are you using in
PainelController?It seems you don’t have
@session_start();in that particular controller.Usually, you can use existing
Authlibraries that will handle this for you, and then include this in the constructor of everycontrollerthat or even inMY_Controller.Here’s an example:
And then my
authlibrary would have a constructor like this.EDIT:
You can also include this in your
autoloadusually located atconfig/autoload.php.http://codeigniter.com/user_guide/general/autoloader.html
Here’s an example.