<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>

CC BY-SA 3.0
2015-06-16T18:02:48.493Z
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.
Bowenac
CC BY-SA 4.0
2018-05-20T21:43:23.513Z
I'd use JavaScript for that
andrew.welch
CC BY-SA 4.0
2015-04-26T01:45:56.023Z
2018-05-20T21:42:57.007Z
2018-05-20T21:42:57.007Z
andrew.welch
<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>

CC BY-SA 4.0
2020-02-18T13:11:28.333Z
can we add the cookie in plaintext? Currently, it encodes the value.
inrsaurabh
CC BY-SA 4.0
2020-02-18T15:02:19.283Z
@inrsaurabh you'd have to create cookies in the vanilla PHP way https://www.w3schools.com/php/func_network_setcookie.asp
José Veríssimo
CC BY-SA 4.0
2019-10-11T10:42:23.267Z
2019-10-11T10:42:23.267Z
José Veríssimo
<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>

CC BY-SA 3.0
2014-07-01T19:18:52.490Z
Here is a conveniently written plugin that does just that: http://straightupcraft.com/craft-plugins/lj-cookies
Brad Bell
CC BY-SA 3.0
2014-07-02T11:14:39.703Z
Perfect, `craft()->userSession->saveCookie` and `craft()->userSession->getCookieValue` is what I was looking for.
Tim Kelty
CC BY-SA 3.0
2014-07-02T11:16:23.073Z
@BradBell that plugin just uses setcookie, I was looking to make it work with the `craft()->security` checks.
Tim Kelty
CC BY-SA 3.0
2014-07-01T16:17:23.977Z
2014-07-01T16:17:23.977Z
Bryan Redeagle
<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>

<plugin-development><cookies>
Prescribed way to set cookies (from plugin or Twig)
# 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**: @BradBell 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**: @inrsaurabh you'd have to create cookies in the vanilla PHP way https://www.w3schools.com/php/func_network_setcookie.asp  

---

