<p>Try this:</p>

<pre><code>$cookie = new HttpCookie('myCookieName', $data, [ 'expire' =&gt; time() + 3600 ]);
craft()-&gt;request-&gt;getCookies()-&gt;add( $cookie-&gt;name, $cookie );
</code></pre>

<p>Your code for reading the cookie is fine.</p>

<p>Also – since you use the term "session value" and tagged your question w/ "security" – I'd like to point out that cookies and session variables are different things. The former is stored on the client (the browser), the latter only on the server.</p>

<p>Unless you specifically need a cookie (e.g. because you want the data to persist a certain amount of time) it might be better to just create a basic session variable, using the <a href="https://buildwithcraft.com/classreference/services/HttpSessionService" rel="nofollow noreferrer">HttpSession</a> service:</p>

<pre><code>craft()-&gt;httpSession-&gt;add( $key, $value ); // Set the session variable
craft()-&gt;httpSession-&gt;get( $key ); // Get the session variable
</code></pre>

<p><a href="https://stackoverflow.com/questions/2240556/when-should-i-use-session-variables-instead-of-cookies">Read more about session variables vs. cookies here</a></p>

CC BY-SA 3.0
2015-03-25T23:47:13.527Z
Perfect - thanks. A session var is exactly what I need.
Russ Back
CC BY-SA 3.0
2015-03-25T23:42:23.770Z
2015-03-25T23:42:23.770Z
2017-05-23T12:38:53.957Z
Mats Mikkel Rummelhoff
<p>I have some data I want to store in session:</p>

<pre><code>$data = array
(
    'id' =&gt; '23539471'
    'token' =&gt; 'cf310b81-9431-4303-804d-c1635ae29278'
);
</code></pre>

<p>In my plugin I am doing this:</p>

<pre><code>new HttpCookie('myCookieName', $data, [ 'expire' =&gt; time() + 3600 ]);
</code></pre>

<p>When I dd() that I can see the cookie as expected:</p>

<pre><code>Craft\HttpCookie#1
(
    [name] =&gt; 'myCookieName'
    [value] =&gt; array
    (
        'id' =&gt; '23539471'
        'token' =&gt; 'cf310b81-9431-4303-804d-c1635ae29278'
    )
    [domain] =&gt; ''
    [expire] =&gt; 1427328767
    [path] =&gt; '/'
    [secure] =&gt; true
    [httpOnly] =&gt; true
    [CComponent:_e] =&gt; null
    [CComponent:_m] =&gt; null
)
</code></pre>

<p>However on the next page load the cookie is null:</p>

<pre><code>$cookie = craft()-&gt;request-&gt;getCookie('myCookieName');
</code></pre>

<p>What am I doing wrong here?</p>

<p>::::EDIT::::</p>

<p>I have also tried this which seems to make the cookie available in the current request, but it's null on the next request:</p>

<pre><code>$cookie = new HttpCookie('myCookieName', $data);
$cookie-&gt;expire = time() + 3600;

craft()-&gt;request-&gt;cookies-&gt;add('myCookieName', $cookie);
</code></pre>

CC BY-SA 3.0
2015-03-25T23:18:20.853Z
I should add that I am on a secure HTTPS page when setting this (the whole site is on HTTPS).
Russ Back
<plugin-development><security><cookies><session>
How should I set an arbitrary session value in a plugin?
# How should I set an arbitrary session value in a plugin?

(Asked by: username_0 on 2015-03-25)

I have some data I want to store in session:
$data = array
(
    'id' => '23539471'
    'token' => 'cf310b81-9431-4303-804d-c1635ae29278'
);

In my plugin I am doing this:
new HttpCookie('myCookieName', $data, [ 'expire' => time() + 3600 ]);

When I dd() that I can see the cookie as expected:
Craft\HttpCookie#1
(
    [name] => 'myCookieName'
    [value] => array
    (
        'id' => '23539471'
        'token' => 'cf310b81-9431-4303-804d-c1635ae29278'
    )
    [domain] => ''
    [expire] => 1427328767
    [path] => '/'
    [secure] => true
    [httpOnly] => true
    [CComponent:_e] => null
    [CComponent:_m] => null
)

However on the next page load the cookie is null:
$cookie = craft()->request->getCookie('myCookieName');

What am I doing wrong here?
::::EDIT::::
I have also tried this which seems to make the cookie available in the current request, but it's null on the next request:
$cookie = new HttpCookie('myCookieName', $data);
$cookie->expire = time() + 3600;

craft()->request->cookies->add('myCookieName', $cookie);

**Tags:** plugin-development, security, cookies, session
> **Comments:**
> **username_0**: I should add that I am on a secure HTTPS page when setting this (the whole site is on HTTPS).  

---

## 1 answers

### Answer by username_1 on 2015-03-25. Score: 3 | Accepted answer

Try this:
$cookie = new HttpCookie('myCookieName', $data, [ 'expire' => time() + 3600 ]);
craft()->request->getCookies()->add( $cookie->name, $cookie );

Your code for reading the cookie is fine.
Also – since you use the term "session value" and tagged your question w/ "security" – I'd like to point out that cookies and session variables are different things. The former is stored on the client (the browser), the latter only on the server.
Unless you specifically need a cookie (e.g. because you want the data to persist a certain amount of time) it might be better to just create a basic session variable, using the HttpSession service:
craft()->httpSession->add( $key, $value ); // Set the session variable
craft()->httpSession->get( $key ); // Get the session variable

Read more about session variables vs. cookies here

> **Comments:**
> **username_0**: Perfect - thanks. A session var is exactly what I need.  

---

