Welcome Guest, Not a member yet? Register   Sign In
Check if user is logged in
#1

[eluser]Gerep[/eluser]
Hi fellas,

It's been a while since I don't use CI and I'm with a starter doubt.

I have a hook that is pre_controller.

Code:
class Validation {
    
    private $CI = get_instance();

    function __construct()
    {
        $this->CI = get_instance();
    }
    
    function test() {
        if(!$this->CI->session->userdata('user')) {
            Template::set('title', 'Login');
            $this->CI->load->view('login');
        }
    }
}

The problem is that as it is pre_controller it will not call its own controller. How to solve this?

Thanks in advance.
#2

[eluser]WanWizard[/eluser]
In pre_controller the CI instance doesn't exist (because the CI instance == the controller), so this is not going to work.
#3

[eluser]InsiteFX[/eluser]
Try using post_controller hook
#4

[eluser]misplacedme[/eluser]
[quote author="InsiteFX" date="1309485184"]Try using post_controller hook[/quote]
That probably wouldn't work since they are validating a user(data needs available to the controller)

I have a user security library that I autoload first. It validates the user and I have all the data available to the controller when the time comes. You might want to check into that. You may also be able to use the hook "post_controller_constructor". I haven't used this hook, so I'm not 100% sure that it loads the CI object.
#5

[eluser]stiginshanghai[/eluser]
Just noticed this post. I was considering making a precontroller for auth check, but new to codeigniter, so wondering what best approach would be.

In above example couldnt one echo a header location, and then exit?

I read most of the userdocs, but cant see if hooks can be matched to urls. F.x. if I make a auth check hook but only want on on say /member/.... url matching. Is that possible, or I need to check that in the hook code?

ps. nice framework, best php MVC I seen so far, nice and clean.




Theme © iAndrew 2016 - Forum software by © MyBB