EllisLab text mark
Advanced Search
     
Check if session is started otherwise redirect to home with message
Posted: 14 April 2012 09:39 AM   [ Ignore ]
Joined: 2010-10-06
117 posts

Hi, I’m trying to check if users has logged in in my site and if not then redirect it to login page but I need this across all the function in my controllers. I don’t want to repeat the code so I think in this:

function __construct() {
        parent
::__construct();
        
        if(!
$this->session->userdata('session_id')) {
            $this
->session->set_flashdata("no_logged_in""No tiene permisos para acceder a esta página, por favor " anchor(site_url() , 'inicie sesión'));
        
}

But it’s not working because users still able to access for example /users/search when this should not be possible until them signin. Any help?

Cheers

 
Posted: 14 April 2012 11:22 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-11-04
244 posts
ReyPM - 14 April 2012 09:39 AM

Hi, I’m trying to check if users has logged in in my site and if not then redirect it to login page but I need this across all the function in my controllers. I don’t want to repeat the code so I think in this:

function __construct() {
        parent
::__construct();
        
        if(!
$this->session->userdata('session_id')) {
            $this
->session->set_flashdata("no_logged_in""No tiene permisos para acceder a esta página, por favor " anchor(site_url() , 'inicie sesión'));
        
}

But it’s not working because users still able to access for example /users/search when this should not be possible until them signin. Any help?

Cheers

because you’re not actually blocking access. All I can see you doing is setting flashdata.
Why don’t you redirect them?

if(!$this->session->userdata('session_id')) {
            $this
->session->set_flashdata("no_logged_in""No tiene permisos para acceder a esta página, por favor " anchor(site_url() , 'inicie sesión'));
redirect('/');
        
 
Posted: 14 April 2012 04:33 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-10-06
117 posts

Didn’t work I can still accessing to any URL even if user isn’t logged in