1

Is it possible for me to execute any "init" like function for my Plugin the first time the user visits the site? You could say when the first userSession is defined.

I tried looking through the docs to see if there was any Event for when the user session is created but I couldn't find any.

I want to add some default values to the userSession the first time the user hits my site.

2

Sessions are initialized by Craft, and plugins are loaded/called after that, so you'll have to assume the session is already up.

Not sure about your use case, but here is how you could execute code once per session:

if(!craft()->httpSession->get('initialized'))
{
    /*
    Your code executed once per session goes here
    */

    craft()->httpSession->add('initialized', true);
}
  • Perfect. I'll use the HttpSessionService, not the userSession then. Thank you Benjamin. – naboovalley Oct 7 '14 at 15:17
0

I would create a new session in this case, it would probably be a bad idea to modify the userSession just in case there is an update that changes it etc.

  • So you would recommend the HttpSessionService for this? Would I run this in my init() function on every request to see if it's initialized or not? – naboovalley Oct 7 '14 at 13:51
  • I think somebody else would be better off answering that, as I've not done it within the context of craft, but if yes basically if your session is not initialised then initialise it. – Shane Hudson Oct 7 '14 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.