Hot answers tagged

7

Correct... craft()->fileCache will save to your craft/storage/runtime/cache folder by default, which will have a .bin file that has a hash of the key you provided when calling set() and the contents will be a serialized version of the value you passed in. Also worth noting that while craft()->fileCache still exists, as of Craft 2.0, it is simply a ...


7

So far the only solution I have found is to use a plugin, and Lewis Jenkins' LJ Cookies plugin does the trick nicely: {{ craft.lj_cookies.get( NAME ) }} It also allows you to set and delete cookies within templates.


6

I wrote a plugin similar to lj_cookies that accomplishes this for both regular and craft secure cookies: Craft 2.x: Cookies Craft 3.x: Cookies ...enjoy.


6

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


5

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 service like Silk Tide Cookie Consent. Both options require you to have access to your website via FTP. For the plugin, you'll upload it to the /craft/...


4

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


4

Let's get the easy question out of the way first... Don't use Craft's localization to handle this task. While the localization feature is amazing and powerful, it doesn't fit the needs you're describing. Localization is mainly geared towards handling different languages for different countries. So what's the best way to handle this? Fortunately, it sounds ...


3

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 actually need session, which is basically what you're looking for.


3

Try this: $cookie = new HttpCookie('myCookieName', $data, [ 'expire' => time() + 3600 ]); craft()->request->getCookies()->add( $cookie->name, $cookie ); Your code for reading the cookie is fine. Also – since you use the term "session value" and tagged your question w/ "security" – I'd like to point out that cookies and session variables are ...


3

You're probably looking for a combination of: craft()->userSession->login() or craft()->userSession->loginByUserId() along with setting the userSessionDuration config setting to a really high value.


3

One option would be to set a query string on your cta urls, have your template look for that, and change your content accordingly. You can't set a cookie in Twig directly but there's a few plugins to help you out, like the handy LJ Cookies. So on your CTA you might do something like: <a href="/cta-link-to/?cta=button2" class="cta-button">Buy now!&...


3

I think that you've outlined the process perfectly. To put the pieces together I would suggest that you create a small plugin (perhaps using the trusty BusinessLogic plugin as a starting point). To communicate with your plugin, add a 'template variable' method to your plugin's variables file, and your geolocation php. For more info see the docs on template ...


3

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 cookies %} {{ cookie.name }} {% endfor %}


3

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 when 'now' is later on? Finally, make sure to use proper names to make your code easier to parse. {# Add other parameters to the query here. #} {% set ...


2

I kind of doubt this is it, but Craft does have a couple of config settings that might be coming into play here. You can tell if you're being affected by these by looking in your craft/storage/runtime/logs files and see if there are any messages being logged with a [warning] related to not being able to restore session. If you are being affected by those, ...


2

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()->...


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


2

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 following method. $cookie = new Cookie(['name' => 'cookieName']); $cookie->value = $data['accessToken']; $cookie->expire = $data['expires']; Craft::$app->...


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 cookie private function _getCraft2CookieHash() { $newBasePath = Craft::$app->getBasePath(); $oldBasePath = str_replace('vendor/craftcms/cms/src', '...


2

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() as $cookie) { $value = $cookie->value; if ($cookie->expire != 1 && isset($validationKey)) { $value = Yii::$app->getSecurity()-&...


2

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', 'purgeInactiveCarts' => true, 'purgeInactiveCartsDuration' => 'P3M', ];


2

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 plugin so I could access it in twig, then appended that query parameter to a donation button on other pages. Cookies: https://plugins.craftcms.com/cookies You ...


1

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 cookie and all other cookies that Craft uses by default here: https://craftcms.com/support/how-does-craft-use-cookies


1

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


1

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 ~ "username" # Craft sessions have the word username in them ) { return (pass); }`


1

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 when they tried to pay: Something changed with the order before payment, please review your order and submit payment again. The secondary issue is allowing ...


1

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 :8000 if '/admin/' is visited from :3000 Here's my whole working browser-sync.js config file, for those few other souls searching for something similar: ...


1

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() (which is just a wrapper for PHP's session_get_cookie_params. From there you could check the existing session cookie domain and if it's site1.domain.com, nuke the ...


1

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 that cookie data hasn't been tampered with between requests. Which is especially useful if, for example, cookies are used to maintain user session. If that's ...


1

Try deleteCookie() in HttpRequestService?


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