We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

How can I test for "if logged in" in an extension?

Development and Programming

Chad Crowell's avatar
Chad Crowell
242 posts
13 years ago
Chad Crowell's avatar Chad Crowell

Using sessions_end hook

Should be pretty simple, right?

if ($this->EE->session->userdata[‘member_id’] == 0) { //user is not logged in }

I’ve also tried group_id

But when I test that by visiting the site while not logged in, I get:

A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$userdata Filename: gcx/ext.gcx.php Line Number: 160

A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$flashdata Filename: libraries/Functions.php Line Number: 368

160 being the line where I am testing for the member_id/group_id

Any ideas?

Here is the entire function: http://pastie.org/private/wwdgsadqyqbgg3gyf3bvzw

       
Rob Sanchez's avatar
Rob Sanchez
335 posts
13 years ago
Rob Sanchez's avatar Rob Sanchez

I think the session object isn’t yet attached to this->EE at that point. But conveniently, you are passed the session object itself as a parameter to sessions_end:

function my_extension_hook_into_sessions_end(&$session)
{
    if ( ! $session->userdata('member_id')) {
        //not logged in
    }
}
       
Chad Crowell's avatar
Chad Crowell
242 posts
13 years ago
Chad Crowell's avatar Chad Crowell

Thanks Rob. No dice. I also used $sess in the function call with no luck. However, if I print_r($sess) I can see the output including userdata and a group_id of 3 and member_id of 0. Something else is amiss on this one…

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
13 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Hey Chad,

Were you able to resolve this?

       
Chad Crowell's avatar
Chad Crowell
242 posts
13 years ago
Chad Crowell's avatar Chad Crowell

Not yet- haven’t had time to revisit. Hopefully Monday.

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
13 years ago
Pascal Kriete's avatar Pascal Kriete

Hey Chad,

What do you get on $sdata?

function sess_end_hook($s)
{
    echo $s->sdata['member_id'];
}
       
Chad Crowell's avatar
Chad Crowell
242 posts
13 years ago
Chad Crowell's avatar Chad Crowell

sdata is within the $sess array. Erik helped me figure this one out - it was choking on the flash data array, which, at the point where the sessions_end hook is run, isn’t instantiated yet. By suppressing errors in my code it passes over this issue and works.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.