<p>You should probably be using something like this:</p>

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

    craft()-&gt;request-&gt;getCookies()-&gt;add($cookie-&gt;name, $cookie);
</code></pre>

<p>Setting whatever properties you want on <a href="http://buildwithcraft.com/classreference/etc/web/HttpCookie" rel="nofollow">HttpCookie</a>.</p>

<p>Using <a href="http://buildwithcraft.com/classreference/services/UserSessionService" rel="nofollow">UserSessionService</a>-><a href="http://buildwithcraft.com/classreference/services/UserSessionService#saveCookie-detail" rel="nofollow">saveCookie</a> is meant mainly for Craft's session identity cookie, so it has some security checks it goes through saving the cookie that <code>craft.request.getCookie('location')</code> is probably failing.  </p>

CC BY-SA 3.0
2014-11-05T19:15:03.913Z
Dear Brad - you're my hero.
Adam McCombs
CC BY-SA 4.0
2018-08-08T05:42:48.433Z
Does this work in Craft 3?  I'm setting a cookie using js-cookie and it's not returning anything in my twig templates.
biko
CC BY-SA 4.0
2018-08-08T06:08:24.933Z
I went for a plugin instead and it's working nicely in Craft 3: https://github.com/nystudio107/cookies
biko
CC BY-SA 3.0
2014-11-05T18:50:41.137Z
2014-11-05T19:15:41.783Z
2014-11-05T19:15:41.783Z
Brad Bell
<p>I wrote a plugin similar to lj_cookies that accomplishes this for both regular and craft secure cookies: <a href="https://github.com/nystudio107/cookies" rel="nofollow noreferrer">Cookies</a></p>

<p><a href="https://github.com/nystudio107/cookies" rel="nofollow noreferrer">https://github.com/nystudio107/cookies</a></p>

<p>...enjoy.</p>

CC BY-SA 4.0
2015-04-26T05:28:27.467Z
2019-03-16T00:32:52.067Z
2019-03-16T00:32:52.067Z
andrew.welch
<p>Following up on this question: <a href="https://craftcms.stackexchange.com/questions/648/prescribed-way-to-set-cookies-from-plugin-or-twig">Prescribed way to set cookies (from plugin or Twig)</a> the following works great.</p>

<pre><code>craft()-&gt;userSession-&gt;saveCookie($cookieName, $data, $duration);
</code></pre>

<p>I can see <strong>Location</strong> being set as my cookie in the Application Log but when I try to use <code>{{ craft.request.getCookie('location') }}</code> It returns blank.</p>

<p>What am I missing here?</p>

<p><strong>Edit</strong> - Upon closer inspection it doesn't look like saveCookie is working correctly either. I see location from when I was using setFlash but I need the data to persist even if the page is reloaded.</p>

<cookies>
Best way to use saveCookie and craft.request.getCookie('location')
# Best way to use saveCookie and craft.request.getCookie('location')

(Asked by: username_0 on 2014-11-05)

Following up on this question: Prescribed way to set cookies (from plugin or Twig) the following works great.
craft()->userSession->saveCookie($cookieName, $data, $duration);

I can see Location being set as my cookie in the Application Log but when I try to use {{ craft.request.getCookie('location') }} It returns blank.
What am I missing here?
Edit - Upon closer inspection it doesn't look like saveCookie is working correctly either. I see location from when I was using setFlash but I need the data to persist even if the page is reloaded.

**Tags:** cookies


---

## 2 answers

### Answer by username_1 on 2014-11-05. Score: 4 | Accepted answer

You should probably be using something like this:
    $cookie = new HttpCookie($name, $value);
    $cookie->expire = time() + 3600;

    craft()->request->getCookies()->add($cookie->name, $cookie);

Setting whatever properties you want on HttpCookie.
Using UserSessionService->saveCookie is meant mainly for Craft's session identity cookie, so it has some security checks it goes through saving the cookie that craft.request.getCookie('location') is probably failing.

> **Comments:**
> **username_0**: Dear Brad - you're my hero.  
> **username_2**: Does this work in Craft 3?  I'm setting a cookie using js-cookie and it's not returning anything in my twig templates.  
> **username_2**: I went for a plugin instead and it's working nicely in Craft 3: https://github.com/nystudio107/cookies  

---

### Answer by username_3 on 2015-04-26. Score: 2

I wrote a plugin similar to lj\_cookies that accomplishes this for both regular and craft secure cookies: [Cookies](https://github.com/nystudio107/cookies)

<https://github.com/nystudio107/cookies>

...enjoy.


---

