Skip to content

Manage Session OR logged_id users. using hooks `post_controller_constructor`.

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

config/autoload.php

$autoload['libraries'] = array('ion_auth');

config/config.php

$config['enable_hooks'] = TRUE;

config/hooks.php

$hook['post_controller_constructor'][] = array(
    "class"    => "Check_session",// any name of class that you want
    "function" => "validate",// a method of class
    "filename" => "Check_session.php",// where the class declared
    "filepath" => "hooks"// this is location inside application folder
);;

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 . '</strong> 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->ion_auth->logged_in())
               {
                     //redirect in login
               }
        }
}