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

Can't access $this->EE->session in session_end hook

Development and Programming

esset's avatar
esset
191 posts
15 years ago
esset's avatar esset

I can’t access $this->EE->session in session_end hook.

When trying to do so in my method I get an error, like so:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: EE::$session

Am I doing something wrong? Do I need another hook?

Thanks

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

The session object is the first parameter of the sessions_end hook call. It’s not yet a property of the EE instance object:

function sessions_end($OBJ)
{
    echo "Your member ID is ".$OBJ->userdata['member_id'];
}
       
esset's avatar
esset
191 posts
15 years ago
esset's avatar esset

I see, cool.

Can I still make changes to it from the first object being sent in? Like so:

$OBJ->set_userdata[‘custom_variable’] = “yes”;

OR

$OBJ->set_flashdata[‘custom_variable’] = “yes”;

Thanks

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

Yeah, just add the by reference operator to the param (&).

function sessions_end(&$session)
{
    echo "Your member ID is ".$session->userdata['member_id'];
}

set_flashdata is a available, but set_userdata is not

$session->set_flashdata('custom_variable', 'yes');
$session->userdata['custom_variable'] = 'yes';
       
esset's avatar
esset
191 posts
15 years ago
esset's avatar esset

Thanks for all the help. Now I’m trying this:

if(!$SESSION->userdata('language_temp'))
        {
            $SESSION->set_userdata('language_temp', "en");
        }

I get this mad error 😊

Fatal error: Call to undefined method EE_Session::set_userdata() i
       
esset's avatar
esset
191 posts
15 years ago
esset's avatar esset

Maybe set_userdata and set_flashdata isn’t available in EE 2.1?

I remember that flashdata wasn’t available at all from the beginning.

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

As a follow on to this - I’m trying to access the logger library to post to the Control Panel Log on sessions_end for a project, but no matter which way I try to load the library / post an item, I get various errors in the CI Log.

Is it possible to reference the logger in the sessions_start/sessions_end hook?

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team
As a follow on to this - I’m trying to access the logger library to post to the Control Panel Log on sessions_end for a project, but no matter which way I try to load the library / post an item, I get various errors in the CI Log. Is it possible to reference the logger in the sessions_start/sessions_end hook?

Can you post your code and error message?

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

Hi Erik,

Sorry for my delay in replying - been out of the office.

I’ve tried various combinations, based on your article on using the log:

function start_process(&$OBJ) {

$this->EE = $OBJ;

$this->EE->load->library(‘logger’); $this->EE->logger->log_action(“Testing new CP log item”);

}

and many other combinations of the $OBJ-> or $this-> etc to no avail.

Most of the errors I’m getting are various types of “Call to undefined method”.

Cheers,

Carl.

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

Carl,

You’re using a sessions hook so the object being passed to you is not the EE super object but rather the session object.

You’ll want to use the get_instance() function in your __construct() method and reference the EE super object that way to load and use the log class.

Make sense?

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

ahhhhh Doh! of course!

Makes perfect sense… I’m just having one of those days/weeks/months! 😊

Cheers,

Carl.

       
Carl W Crawley's avatar
Carl W Crawley
136 posts
15 years ago
Carl W Crawley's avatar Carl W Crawley

Just as a follow up….

I’ve created the __construct() function as follows:

function __construct() { $this->EE =& get_instance(); }

and then within the function

function send_process(&$OBJ) { $this->EE->load->library(‘logger’); $this->EE->logger->log_action(“Testing Testing 123”); }

However, I’m getting the following error:

Severity: Notice –> Undefined property: Addons_extensions::$session /home/clients/public_html/sandbox/system/expressionengine/libraries/Logger.php 62

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

Carl,

Since you’re within one of the sessions hook, the session object isn’t actually available within the EE object. This presents an interesting challenge since the log_action() method is referring to the session object.

I’m not sure of the plausibility of this and I also don’t know what it could hurt without further investigation - BUT - this should work:

EE->session =& $OBJ;

   $this->EE->load->library('logger');
   $this->EE->logger->log_action('Testing Testing 123');

   unset($this->EE->session);

}            
?>

I have the unset() function in there just because I haven’t thought through any negatives to defining the EE session object from within the extension hook. Probably best you get a product developer to peek at this as well (Greg, Pascal, Wes etc)

       
iain's avatar
iain
317 posts
14 years ago
iain's avatar iain

Just a note incase anyone else is head scratching wondering why @rsan’s example doesn’t work…

If you have Low Seg2Cat installed, that has priority 1 set and any other extensions using the same sessions_end hook will result in “Trying to get property of non-object” warnings.

I knocked the priority of Low’s extension down to 10 and all worked as described above.

       

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.