Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T19:15:03.913Z
Answers.Text: Dear Brad - you're my hero.
Answers.UserDisplayName: Adam McCombs
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-08T05:42:48.433Z
Answers.Text: Does this work in Craft 3?  I'm setting a cookie using js-cookie and it's not returning anything in my twig templates.
Answers.UserDisplayName: biko
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-08T06:08:24.933Z
Answers.Text: I went for a plugin instead and it's working nicely in Craft 3: https://github.com/nystudio107/cookies
Answers.UserDisplayName: biko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T18:50:41.137Z
Answers.LastActivityDate: 2014-11-05T19:15:41.783Z
Answers.LastEditDate: 2014-11-05T19:15:41.783Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2015-04-26T05:28:27.467Z
Answers.LastActivityDate: 2019-03-16T00:32:52.067Z
Answers.LastEditDate: 2019-03-16T00:32:52.067Z
Answers.UserDisplayName: andrew.welch
Body: <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>
  
Tags: <cookies>
Title: Best way to use saveCookie and craft.request.getCookie('location')
ThreadText: # 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)](https://craftcms.stackexchange.com/questions/648/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](http://buildwithcraft.com/classreference/etc/web/HttpCookie).
  
  Using [UserSessionService](http://buildwithcraft.com/classreference/services/UserSessionService)->[saveCookie](http://buildwithcraft.com/classreference/services/UserSessionService#saveCookie-detail) 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 username_1 - 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.
  
  
  ---
  
  