Welcome Guest, Not a member yet? Register   Sign In
Need tips about authentication check (cookies)
#1

(This post was last modified: 11-03-2015, 02:24 AM by Rashid.)

What is the most common and robust auth check pattern in CI?

I personally see two ways:

1) insert check routine into a hook-function ('pre_controller' or 'post_controller_constructor' ?) - one for all controllers - nice
2) write it in __construct() directly, but it forces to duplicate code a lot, for each controller. bad..

I tried 1st way but got this:

PHP Code:
$hook['pre_controller'] = function ()
{
    
$id $this->input->cookie('user_id'); // ERROR: Undefined property: CI_Hooks::$input
    // ...
}; 

It seems that $this->input is absent in function's scope. Maybe I should create standalone class? or entirely different approach?

Please nudge me in a right direction.
Reply
#2

Create a MY_Controller and do your checks in it, then extend all your other controller's from your MY_Controller.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Besides the fact that you want to check a user id from a cookie i think you are on the right path.
Try the following:

in your config/hooks.php

PHP Code:
$hook['pre_controller'][] = array(
    
"class" => "AppCookieValidator",
    
"function" => "initialize",
    
"filename" => "AppCookieValidator.php",
    
"filepath" => "hooks"
); 


and create a File named AppCookieValidator.php in the application/hooks/ directory and do something like this

PHP Code:
class AppCookieValidator
{

    private 
$ci;

    public function 
__construct()
    {
        
$this->ci = &get_instance();
    }
    
    public function 
initialize()
    {
        
$id $this->ci->input->cookie('user_id'); 
    }

Reply
#4

Thank you, guys!
Reply
#5

(This post was last modified: 12-29-2015, 09:14 AM by Rashid.)

(11-03-2015, 04:45 AM)sintakonte Wrote:
PHP Code:
$hook['pre_controller'][] = array(
 
"class" => "AppCookieValidator",
 
"function" => "initialize",
 
"filename" => "AppCookieValidator.php",
 
"filepath" => "hooks"
); 

Tried it. Should be $hook['post_controller_constructor'] otherwise get_instance() returns NULL.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB