Welcome Guest, Not a member yet? Register   Sign In
Checking a session in a hook
#11

[eluser]ggoforth[/eluser]
[quote author="devvee" date="1241612572"]
I ran my hooks both post_controller_constructor and pre_controller, both gave me the same error. My session library is autoloaded.

If you run the login check post_controller, doesn't that mean code inside a controller gets executed before the person gets sent back to the login page? So if, for instance, you were to access a page that upon accessing performs a certain action, it would do so even if you weren't logged in?

And in that sense, wouldn't it be faster to take care of all these things pre_controller or at least post_controller_constructor?[/quote]

I believe (DISCLAMER: I'm not positive) that your post construct hook gets executed BEFORE any page code runs. If you have code in your construct then yes, that would be run, then your hook would run.

If your hook redirects it would redirect before your code is run. That's my understanding at least. If someone more knowledgable than I has an Idea I would love to hear it. Also, Damien's method accomplishes the same thing just in a little different way. I talked to him a bit via PM about how his method works and it's quite smart. Essentially you have one controller that extends Controller. In this controller you run any login checks, etc.. Then all your other controllers extend that controller.

Example: (Damien, correct me if I'm wrong)

Code:
class Login_check extends Controller{
    function __construct(){
        parent::Controller();
        //run any checks here, or call other functions to run checks
    }
}

class Whatever extends Login_check{
    function index(){
        //do something here
    }
}

If you went this route, your programmers can stay withing the same idea, just extending a different controller.

Hope I'm making sense.

Greg
#12

[eluser]devvee[/eluser]
[quote author="ggoforth" date="1241613025"][quote author="devvee" date="1241612572"]
I ran my hooks both post_controller_constructor and pre_controller, both gave me the same error. My session library is autoloaded.

If you run the login check post_controller, doesn't that mean code inside a controller gets executed before the person gets sent back to the login page? So if, for instance, you were to access a page that upon accessing performs a certain action, it would do so even if you weren't logged in?

And in that sense, wouldn't it be faster to take care of all these things pre_controller or at least post_controller_constructor?[/quote]

I believe (DISCLAMER: I'm not positive) that your post construct hook gets executed BEFORE any page code runs. If you have code in your construct then yes, that would be run, then your hook would run.

If your hook redirects it would redirect before your code is run. That's my understanding at least. If someone more knowledgable than I has an Idea I would love to hear it. Also, Damien's method accomplishes the same thing just in a little different way. I talked to him a bit via PM about how his method works and it's quite smart. Essentially you have one controller that extends Controller. In this controller you run any login checks, etc.. Then all your other controllers extend that controller.

Example: (Damien, correct me if I'm wrong)

Code:
class Login_check extends Controller{
    function __construct(){
        parent::Controller();
        //run any checks here, or call other functions to run checks
    }
}

class Whatever extends Login_check{
    function index(){
        //do something here
    }
}

If you went this route, your programmers can stay withing the same idea, just extending a different controller.

Hope I'm making sense.

Greg[/quote]

Thanks for the response, I will definitely look into that!

I changed my approach and gave up on the hook idea. I included an authentication library in every controller. All I have to do is load the library into the constructor of the controller if the page is supposed to be visible only to logged in users. The constructor of the library automatically checks whether the user is logged in or not and redirects accordingly.

I can pretty much autoload the library and get it to work the same way as I intended the hook to work, or just include it in the controllers I want to be protected Smile




Theme © iAndrew 2016 - Forum software by © MyBB