Answers.Body: <p>Sessions are initialized by Craft, and plugins are loaded/called after that, so you'll have to assume the session is already up.</p>
  
  <p>Not sure about your use case, but here is how you could execute code once per session:</p>
  
  <pre><code>if(!craft()-&gt;httpSession-&gt;get('initialized'))
  {
      /*
      Your code executed once per session goes here
      */
  
      craft()-&gt;httpSession-&gt;add('initialized', true);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-07T15:17:00.483Z
Answers.Text: Perfect. I'll use the HttpSessionService, not the userSession then. Thank you Benjamin.
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-07T14:50:43.857Z
Answers.LastActivityDate: 2014-10-07T14:50:43.857Z
Answers.UserDisplayName: Benjamin David
Answers.Body: <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-07T13:51:30.440Z
Answers.Text: 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?
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-07T13:52:46.273Z
Answers.Text: 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.
Answers.UserDisplayName: Shane Hudson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-07T13:49:00.107Z
Answers.LastActivityDate: 2014-10-07T13:49:00.107Z
Answers.UserDisplayName: Shane Hudson
Body: <p>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.</p>
  
  <p>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.</p>
  
  <p>I want to add some default values to the userSession the first time the user hits my site.</p>
  
Tags: <plugin-development>
Title: Execute Plugin init function one time
ThreadText: # Execute Plugin init function one time
  
  (Asked by: username_0 on 2014-10-07)
  
  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.
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2014-10-07. Score: 2 | Accepted answer
  
  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);
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Perfect. I'll use the HttpSessionService, not the userSession then. Thank you username_1.  
  
  ---
  
  ### Answer by username_2 on 2014-10-07. Score: 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.
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_2**: 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.  
  
  ---
  
  