Answers.Body: <p>I wrote a plugin similar to lj_cookies that accomplishes this for both regular and craft secure cookies: </p>
  
  <ul>
  <li>Craft 2.x: <a href="https://github.com/nystudio107/cookies" rel="nofollow noreferrer">Cookies</a></li>
  <li>Craft 3.x: <a href="https://github.com/nystudio107/craft-cookies" rel="nofollow noreferrer">Cookies</a></li>
  </ul>
  
  <p>...enjoy.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T18:02:48.493Z
Answers.Text: Is it possible to use this on a click event? I want to set some cookies on CTA's and trying to think of a way to handle that.
Answers.UserDisplayName: Bowenac
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-20T21:43:23.513Z
Answers.Text: I'd use JavaScript for that
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2015-04-26T01:45:56.023Z
Answers.LastActivityDate: 2018-05-20T21:42:57.007Z
Answers.LastEditDate: 2018-05-20T21:42:57.007Z
Answers.UserDisplayName: andrew.welch
Answers.Body: <p>In the new Craft 3, you can use the following in your plugin:</p>
  
  <pre><code>// Create cookie object.
  $cookie = Craft::createObject([
      'class' =&gt; 'yii\web\Cookie',
      'name' =&gt; 'CookiesAccepted', 
      'httpOnly' =&gt; true,
      'value' =&gt; 'true',
      'expire' =&gt; time() + (86400 * 365),
  ]);
  
  // Set cookie.
  Craft::$app-&gt;getResponse()-&gt;getCookies()-&gt;add($cookie);
  </code></pre>
  
  <p>To get the cookie's value, use:</p>
  
  <pre><code>Craft::$app-&gt;request-&gt;cookies-&gt;get('CookiesAccepted');
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-18T13:11:28.333Z
Answers.Text: can we add the cookie in plaintext? Currently, it encodes the value.
Answers.UserDisplayName: inrsaurabh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-18T15:02:19.283Z
Answers.Text: @inrsaurabh you'd have to create cookies in the vanilla PHP way https://www.w3schools.com/php/func_network_setcookie.asp
Answers.UserDisplayName: José Veríssimo
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-11T10:42:23.267Z
Answers.LastActivityDate: 2019-10-11T10:42:23.267Z
Answers.UserDisplayName: José Veríssimo
Answers.Body: <p>Cookies cannot be set via templates. However, in a plugin you have access to Craft's services. The <code>UserSessionService</code> has the <code>saveCookie</code> function.</p>
  
  <pre><code>craft()-&gt;userSession-&gt;saveCookie($cookieName, $data, $duration);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-01T19:18:52.490Z
Answers.Text: Here is a conveniently written plugin that does just that: http://straightupcraft.com/craft-plugins/lj-cookies
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-02T11:14:39.703Z
Answers.Text: Perfect, `craft()->userSession->saveCookie` and `craft()->userSession->getCookieValue` is what I was looking for.
Answers.UserDisplayName: Tim Kelty
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-02T11:16:23.073Z
Answers.Text: @BradBell that plugin just uses setcookie, I was looking to make it work with the `craft()->security` checks.
Answers.UserDisplayName: Tim Kelty
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-01T16:17:23.977Z
Answers.LastActivityDate: 2014-07-01T16:17:23.977Z
Answers.UserDisplayName: Bryan Redeagle
Body: <p>From either a plugin or a Twig template, is there a prescribed way to set (and get) cookies?</p>
  
  <p>I've seen <a href="https://github.com/lewisjenkins/craft-lj-cookies/" rel="nofollow noreferrer">https://github.com/lewisjenkins/craft-lj-cookies/</a>, which just uses PHP's <code>setcookie</code>.</p>
  
  <p>Also noticed <code>getCookie</code> in <code>craft.request</code>. <a href="https://craftcms.com/docs/templating/craft.request#getCookie" rel="nofollow noreferrer">https://craftcms.com/docs/templating/craft.request#getCookie</a></p>
  
  <p>I've noticed the cookies set with <code>setcookie</code> aren't readable by <code>getCookie</code>. How should one set a cookie so that it is readable by <code>getCookie</code>?</p>
  
Tags: <plugin-development><cookies>
Title: Prescribed way to set cookies (from plugin or Twig)
ThreadText: # Prescribed way to set cookies (from plugin or Twig)
  
  (Asked by: username_0 on 2014-07-01)
  
  From either a plugin or a Twig template, is there a prescribed way to set (and get) cookies?
  
  I've seen <https://github.com/lewisjenkins/craft-lj-cookies/>, which just uses PHP's `setcookie`.
  
  Also noticed `getCookie` in `craft.request`. <https://craftcms.com/docs/templating/craft.request#getCookie>
  
  I've noticed the cookies set with `setcookie` aren't readable by `getCookie`. How should one set a cookie so that it is readable by `getCookie`?
  
  **Tags:** plugin-development, cookies
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_5 on 2014-07-01. Score: 6 | Accepted answer
  
  Cookies cannot be set via templates. However, in a plugin you have access to Craft's services. The `UserSessionService` has the `saveCookie` function.
  
  ```
  craft()->userSession->saveCookie($cookieName, $data, $duration);
  
  ```
  
  > **Comments:**
  > **username_6**: Here is a conveniently written plugin that does just that: http://straightupcraft.com/craft-plugins/lj-cookies  
  > **username_0**: Perfect, `craft()->userSession->saveCookie` and `craft()->userSession->getCookieValue` is what I was looking for.  
  > **username_0**: @username_6 that plugin just uses setcookie, I was looking to make it work with the `craft()->security` checks.  
  
  ---
  
  ### Answer by username_1 on 2015-04-26. Score: 6
  
  I wrote a plugin similar to lj\_cookies that accomplishes this for both regular and craft secure cookies:
  
  * Craft 2.x: [Cookies](https://github.com/nystudio107/cookies)
  * Craft 3.x: [Cookies](https://github.com/nystudio107/craft-cookies)
  
  ...enjoy.
  
  > **Comments:**
  > **username_2**: Is it possible to use this on a click event? I want to set some cookies on CTA's and trying to think of a way to handle that.  
  > **username_1**: I'd use JavaScript for that  
  
  ---
  
  ### Answer by username_3 on 2019-10-11. Score: 4
  
  In the new Craft 3, you can use the following in your plugin:
  
  ```
  // Create cookie object.
  $cookie = Craft::createObject([
      'class' => 'yii\web\Cookie',
      'name' => 'CookiesAccepted', 
      'httpOnly' => true,
      'value' => 'true',
      'expire' => time() + (86400 * 365),
  ]);
  
  // Set cookie.
  Craft::$app->getResponse()->getCookies()->add($cookie);
  
  ```
  
  To get the cookie's value, use:
  
  ```
  Craft::$app->request->cookies->get('CookiesAccepted');
  
  ```
  
  > **Comments:**
  > **username_4**: can we add the cookie in plaintext? Currently, it encodes the value.  
  > **username_3**: @username_4 you'd have to create cookies in the vanilla PHP way https://www.w3schools.com/php/func_network_setcookie.asp  
  
  ---
  
  