<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>

CC BY-SA 3.0
2014-10-07T15:17:00.483Z
Perfect. I'll use the HttpSessionService, not the userSession then. Thank you Benjamin.
naboovalley
CC BY-SA 3.0
2014-10-07T14:50:43.857Z
2014-10-07T14:50:43.857Z
Benjamin David
<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>

CC BY-SA 3.0
2014-10-07T13:51:30.440Z
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
CC BY-SA 3.0
2014-10-07T13:52:46.273Z
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
CC BY-SA 3.0
2014-10-07T13:49:00.107Z
2014-10-07T13:49:00.107Z
Shane Hudson
<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>

<plugin-development>
Execute Plugin init function one time
# 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 Benjamin.  

---

### 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.  

---

