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

[eluser]devvee[/eluser]
[quote author="ggoforth" date="1241612259"][quote author="devvee" date="1241611969"]I've tried using the CI object:

Code:
var $CI
$CI =& get_instance();

echo $this->CI->session->userdata('username');

This gives me the same error. I forgot to mention that the controller I'm trying to call also tries to use $this->session->userdata('username');

The application I'm working on is supposed to be completely closed off from the outside and not a single controller that will be added in the future will be visible without first having logged in.

I chose to do it with hooks because that way other colleagues don't have to stray off the default code igniter way by extending a custom controller or by having to put a login check in every controller.

Regardless of whether this is good practice or not (since I read about hooks being seen as obfuscated/invisible code and I agree), I'd like to solve this issue so I won't be having it in the future Wink

Thanks for all the replies though![/quote]

Did you try running your hook at the post_controller_constructor instead of pre controller? Also, when are you loading your session library? Is it autoloaded or loaded manually in the controller? If your going to stick to hooks (with Damien's method being the alternative) just run it AFTER the constructor, that way you will have access to $this->session->userdata(). If your doing it pre controller the CI object hasn't been created yet, so you won't be able to access it.

FYI, I'm doing it this exact way and I know it works. I run a check post construct and if they aren't logged in, back to the login they go.

Greg[/quote]
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?


Messages In This Thread
Checking a session in a hook - by El Forum - 05-05-2009, 08:56 AM
Checking a session in a hook - by El Forum - 05-05-2009, 08:59 AM
Checking a session in a hook - by El Forum - 05-05-2009, 02:59 PM
Checking a session in a hook - by El Forum - 05-05-2009, 03:06 PM
Checking a session in a hook - by El Forum - 05-05-2009, 03:35 PM
Checking a session in a hook - by El Forum - 05-05-2009, 03:41 PM
Checking a session in a hook - by El Forum - 05-05-2009, 03:44 PM
Checking a session in a hook - by El Forum - 05-06-2009, 01:12 AM
Checking a session in a hook - by El Forum - 05-06-2009, 01:17 AM
Checking a session in a hook - by El Forum - 05-06-2009, 01:22 AM
Checking a session in a hook - by El Forum - 05-06-2009, 01:30 AM
Checking a session in a hook - by El Forum - 05-06-2009, 04:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB