5 votes
Accepted

Show content since last visit

First, you want to always set the cookie to update it to the current date. Otherwise, the user will see content marked as new repeatedly. Also, setting 'now' as a string won't work – how will you know ...
MoritzLost's user avatar
  • 11.3k
5 votes
Accepted

Greetings - about cookies?

As Ryan has said in the comments, there currently isn't a native cookie consent feature within Craft CMS. You'll need to either install a plugin (Cookie Consent plugin for Craft CMS is good) or use a ...
Sam's user avatar
  • 421
4 votes

Prescribed way to set cookies (from plugin or Twig)

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', '...
José Veríssimo's user avatar
3 votes

How can I get cookies that were set in javascript from a twig template? [NEW]

Assuming this is for Craft 3, you can do something like this from a template to loop through all available cookies for a site: {% set cookies = craft.app.request.getCookies() %} {% for cookie in ...
Brad Bell's user avatar
  • 67.5k
3 votes
Accepted

How can I disable sessions for most of my Craft site?

I'm not sure there is going to be an easy way to do this in Craft 2, unfortunately. In the upcoming Craft 3, we refactored the session logic so that a session will only get started on requests that ...
Brad Bell's user avatar
  • 67.5k
2 votes

How can I delete a cookie from a plugin?

I wasn't able to find an answer for removing a cookie in Craft 3 so after plenty of time of trying to figuring it out, this is my conclusion. First of all, I am setting the cookie using the ...
Jamie Hunt's user avatar
2 votes

Craft 3: Recognize cookies created in Craft 2

After a lot of trial & error, it turns out this is not possible. (At least, not by me.) I was able to retrieve the raw Craft 2 cookie data using the following code... // Generate hash of Craft 2 ...
Lindsey D's user avatar
  • 24k
2 votes

How to add CSRF cookie directive?

It is actually possible but it's a little bit difficult for beginners. You'll need make use of Service Locators and overwrite craft\web\Response to change this method foreach ($this->getCookies() ...
Robin Schambach's user avatar
2 votes
Accepted

Campaign UTM Tracking through to form

Yes, you can do something like that. I built something very similar a few months ago where I checked for a query parameter in the URL when the page loaded, created a cookie with it using the Cookies ...
darylknight's user avatar
  • 3,290
2 votes
Accepted

Craft 2 Commerce 1 cart session expiry

Create a commerce.php config file in your config folder and set the cartCookieDuration to a time interval. The default is 3 months. <?php return [ 'cartCookieDuration' => 'P3M', '...
Luke Holder's user avatar
  • 6,827
2 votes

How can I get cookies that were set in javascript from a twig template? [NEW]

If you want a specific cookie instead of the set of cookies, you'd use Old Craft 3 Syntax: {% set specificCookie = craft.app.request.getCookie('specificCookie') %} Deprecation Replacement (for Craft ...
cherrykoda's user avatar
  • 1,059
2 votes

Not instantiable – yii\di\NotInstantiableException when setting and reading cookies

So your code should actually be: {% set cookie = create({ class: 'yii\\web\\Cookie', name: 'letzterBesuch', value: now | atom, }) %} Notice the ...
andrew.welch's user avatar
  • 11.6k
1 vote

Cookies Change-If we add(/?) with URL content changes are reflected

Looks like you're using Cloudflare caching, and also the Blitz plugin. You'll need to find a way to clear both those caches after making changes.
James Smith's user avatar
  • 5,189
1 vote

Can't access Cookie from controller in plugin

Craft uses Yii's cookie handling, which includes cookie validation, resulting in cookies being signed with a hash string. To retrieve a plaintext (unvalidated) cookie, use PHP's global variable $...
Ben Croker's user avatar
  • 7,411
1 vote
Accepted

Cookie-Plugin: Why does Cookies get closing by change entry via navigation / menu?

Here's how I approach what I think you're trying to do. I have a global Notice setup, which includes a lightswitch field called showNotice, a heading called noticeTitle and a text field called ...
Clive Portman's user avatar
1 vote

CRAFT_CSRF_TOKEN is failing to write

Ok - think I figured this out. Our dev site is hosted on a sub-domain of our production site, which is served over HTTPS. Seems like the CRAFT_CSRF_TOKEN cookie set by our production site is the ...
Jegra's user avatar
  • 81
1 vote

How to implement "cookie consent message" to visitors of our website using Craft 3

Just search for "Cookie" in the plugin store. There are several free ones along with some paid. I have used the Cookie Consent Banner on a couple sites.
Jim Wyse's user avatar
  • 451
1 vote
Accepted

Craft Cookies Plugin: Font Loading Detection

So I'm not sure what you're accomplishing with this line: {% do setCookie('fonts-loaded', fontLoad, now | date_modify("+1 hour").timestamp) %} I guess trying to give the cookie a longer duration ...
andrew.welch's user avatar
  • 11.6k
1 vote
Accepted

How to set cookie to only show intro animation once per session

You could try something like {% set vidAlreadyPlayed = getSecureCookie('intro-animation') %} {% if not vidAlreadyPlayed %} {{ setSecureCookie('intro-animation', 'hey', now | ...
alexr's user avatar
  • 608
1 vote

Cookiebot finding TS# cookie

Craft doesn't natively set a cookie named TS#, so it's either coming from something in your front-end templates or possibly from a plugin you've got installed. You can read about the CraftSessionId ...
Brad Bell's user avatar
  • 67.5k
1 vote

Craft 3: how can I auto-redirect to a site version using the user IP?

Country Redirect will let you do that without any latency from external services, with the soon-to-be-released Craft 3 version (I'll update the answer once its out).
Fred Carlsen's user avatar
  • 2,997
1 vote
Accepted

Session cookies

You could write a plugin and put some code in it's init() method which would get fired on every request. In the init() method, that plugin could call craft()->httpSession->getCookieParams() (...
Brad Bell's user avatar
  • 67.5k
1 vote
Accepted

Why does the UserSessionService create a *state* cookie?

craft()->userSesssion->saveCookie() is basically a fancy wrapper for craft()->request->getCookies()->add() that (as you've discovered) does some key and value based hashing to ensure ...
Brad Bell's user avatar
  • 67.5k
1 vote

Bypass Varnish cache if user is logged in

So the answer is to test the cookie for a name with username in it, and pass if it exists. You could also check for an exactly 32 digit alphanumeric cookie name I guess. if ( req.http.Cookie ~ "...
Richard Frank's user avatar
1 vote

Craft Commerce cart timing out but allowing order to be processed

This is likely due to the item going out of stock while they were away from the cart, then when they went to pay, the item had to be removed from the cart, but they should have seen an error message ...
Luke Holder's user avatar
  • 6,827
1 vote
Accepted

"An unknown error occurred." appears when I try to log into Craft's admin using browsersync's proxy mode

Forgot about this question! I've since solved it by using a different approach. Now I'm injecting the broser-sync script via its config, avoiding injection if the url is '/admin/', and redirecting to ...
Ole's user avatar
  • 245
1 vote
Accepted

How can I delete a cookie from a plugin?

Try deleteCookie() in HttpRequestService?
andrew.welch's user avatar
  • 11.6k
1 vote

How can I get cookies that were set in javascript from a twig template?

For now, you have to use a plugin I recommend one of these 2 Craft Cookies Plugin https://github.com/nystudio107/craft-cookies Craft LJ Cookies Plugin https://github.com/khamer/craft-lj-cookies
Tunji Oyeniran's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible