Welcome Guest, Not a member yet? Register   Sign In
Hooks example
#3

[eluser]SabbeRubbish[/eluser]
Hi freshface,
I've been a CI user for only 1 day and I've already needed a hook.
Was scratching the back of my head to figure out how to get the session or db as just using $this-> wouldn't work of course.

So I'll show you how I've done my authentication hook for every page, loaded as post_controller_constructor:

Code:
$hook['post_controller_constructor'] = array(
                                'class'    => 'Hooks',
                                'function' => 'session_check',
                                'filename' => 'hooks.php',
                                'filepath' => 'hooks',
                                'params'   => array()
                                );
Code:
<?php
class Hooks {
    var $CI;

    function Hooks() {
        $this->CI =& get_instance();
    }
    
    function session_check() {
        if(!$this->CI->session->userdata("logged_in") && $this->CI->uri->uri_string != "/user/login")
            redirect('user/login', 'location');
    }
}

?>

Use this at your own risk, I'm still going to change the way my session data is stored (although it's not mission critical and I've got it slightly encrypted, but hey...)

Hope you can use this!


Messages In This Thread
Hooks example - by El Forum - 02-14-2008, 12:13 PM
Hooks example - by El Forum - 02-15-2008, 11:17 AM
Hooks example - by El Forum - 04-13-2008, 10:25 AM
Hooks example - by El Forum - 04-13-2008, 02:00 PM
Hooks example - by El Forum - 04-14-2008, 02:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB