<p>Go to <a href="https://pluginfactory.io/">https://pluginfactory.io/</a> to generate a boilerplate Craft plugin.</p>

<ul>
<li>API Version: Craft CMS version 2.5.x</li>
<li>PluginName: Twig Session</li>
<li>Description: Anything</li>
<li>Version: 1.0.0</li>
<li>Plugin Author: Anything</li>
<li>Author URL: Anything</li>
<li>Github Name: Blank</li>
<li>Uncheck: Tweet it out</li>
<li>Check: Variables</li>
<li>Click: Build Plugin</li>
</ul>

<p>Download <code>twigsession.zip</code> and unzip it. Place the unzipped <code>twigsession</code> folder in your <code>craft/plugins</code> folder.  Open <code>craft/plugins/twigsession/variables/TwigSessionVariable.php</code> and change:</p>

<pre><code>public function exampleVariable($optional = null)
{
    return "And away we go to the Twig template...";
}
</code></pre>

<p>To:</p>

<pre><code>public function add($key, $value)
{
    craft()-&gt;httpSession-&gt;add($key, $value)
}
</code></pre>

<p>And add a new:</p>

<pre><code>public function get($key)
{
    return craft()-&gt;httpSession-&gt;get($key)
}
</code></pre>

<p>Go to Settings->Plugins in the control panel. Install the "Twig Session" plugin.</p>

<p>From your front end templates, you can now use <code>craft.twigSession.add('foo', 'bar')</code> and <code>craft.twigSession.get('foo')</code>.</p>

CC BY-SA 3.0
2016-09-09T20:45:41.590Z
+1 You basically just wrote this plugin.
Lindsey D
CC BY-SA 3.0
2016-09-13T07:42:59.510Z
wow thanks a lot!
Branko
CC BY-SA 3.0
2016-09-09T17:23:03.773Z
2016-09-09T17:23:03.773Z
Brad Bell
<p>I am trying to create the PHP <code>$_SESSION</code> equivalent in Craft CMS using Twig.
I have searched very long and I can't find how you can create sessions in Twig.</p>

<p>In previous posts I have found that I should edit some PHP controllers for this to work but I have just started using Craft and Twig and have no idea what controllers to edit to let this work.</p>

<p>So if someone has an solution to this can you please explain it like you are explaining it to a 2 year old kid?</p>

<p>Thanks in advance! </p>

<controller><twigextension>
Creating sessions in Twig
# Creating sessions in Twig

(Asked by: username_0 on 2016-09-09)

I am trying to create the PHP `$_SESSION` equivalent in Craft CMS using Twig.
I have searched very long and I can't find how you can create sessions in Twig.

In previous posts I have found that I should edit some PHP controllers for this to work but I have just started using Craft and Twig and have no idea what controllers to edit to let this work.

So if someone has an solution to this can you please explain it like you are explaining it to a 2 year old kid?

Thanks in advance!

**Tags:** controller, twigextension


---

## 1 answers

### Answer by username_1 on 2016-09-09. Score: 9 | Accepted answer

Go to https://pluginfactory.io/ to generate a boilerplate Craft plugin.

API Version: Craft CMS version 2.5.x
PluginName: Twig Session
Description: Anything
Version: 1.0.0
Plugin Author: Anything
Author URL: Anything
Github Name: Blank
Uncheck: Tweet it out
Check: Variables
Click: Build Plugin

Download twigsession.zip and unzip it. Place the unzipped twigsession folder in your craft/plugins folder.  Open craft/plugins/twigsession/variables/TwigSessionVariable.php and change:
public function exampleVariable($optional = null)
{
    return "And away we go to the Twig template...";
}

To:
public function add($key, $value)
{
    craft()->httpSession->add($key, $value)
}

And add a new:
public function get($key)
{
    return craft()->httpSession->get($key)
}

Go to Settings->Plugins in the control panel. Install the "Twig Session" plugin.
From your front end templates, you can now use craft.twigSession.add('foo', 'bar') and craft.twigSession.get('foo').

> **Comments:**
> **username_2**: +1 You basically just wrote this plugin.  
> **username_0**: wow thanks a lot!  

---

