Skip to content

Check Session in Multiple Controllers

Lloric Mayuga Garcia edited this page Apr 19, 2017 · 2 revisions

config/config.php

$config['enable_hooks'] = TRUE;

config/hooks.php

$hook['post_controller_constructor'][] = array(
    "class"    => "Check_session",
    "function" => "validate",
    "filename" => "Check_session.php",
    "filepath" => "hooks"
);;

hooks/Check_session.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Check_session
{
        public function __construct()
        {
                
        }

        public function __get($property)
        {
                if ( ! property_exists(get_instance(), $property))
                {
                        show_error('property: ' . strong($property) . ' not exist.');
                }
                return get_instance()->$property;
        }
        public function validate()
        {
               if (in_array($this->router->class, array("login")))//login is a sample login controller
                {
                        return;
                }
               if(!$this->session->has_userdata('user_id'))
               {
                     //redirect in login
               }
        }
}
Clone this wiki locally