How do I set a cookie that expires after a certain time using Tritium? - cookies

I'd like to create a cookie that expires within 5 minutes of the user visiting my page.
I know about add_cookie() function, but have no idea how to set it to expire.

You can set cookies in the scripts/response_main.ts file as such:
set_cookie("cookie_name", "cookie_val")
cookie("cookie_name") {
cookie.expires("Wed, 09 Jun 2021 10:18:14 GMT")
}
Check out this link for more info.

Related

How to completely clean up Google Analytics cookies (including dynamically set ones like _gali)

Since GDPR we all have cookie preferences and obliged to remove cookies if user withdraw their consent. However, if you have Google Analytics (www.googletagmanager.com/gtag) and clean cookies on a button press event, you can find out after page reload that you still have some GA cookies like _gali and _ga_XXXXXX. So, the question is how to kill these?
Apparently GA sets some cookies on document unload (or beforeunload) event. So if you clean up on unload event it works then:
function removeAllCookies() {
document.cookie.split( ";" ).forEach( cookie => {
const [ name ] = cookie.split( "=" );
document.cookie = name + `=; path=/; domain=.YOUR-DOMAIN.com;expires=Thu, 01 Jan 1970 00:00:00 GMT`;
document.cookie = name + "=; path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT";
});
}
window.addEventListener("unload", () => removeAllCookies() );

Failing to delete a cookie using JavaScript - what am I doing wrong?

I'm trying to delete a cookie in Android Chrome (latest version).
DevTools (phone connected to PC via USB) shows the following cookies when I navigate my phone to www.domain.com/admin/clearcookies:
Name Value Domain Path Expires
data-cookie-name foo www.domain.com / 2020-09-16T07:57:01.000Z
data-cookie-name bar .domain.com / 2021-03-24T09:03:04.000Z
On page load I run the following javascript:
function deleteOldWwwScopedCookiesThatWereConflictingWithNewNonWwwCookiesThatISwitchedToBecauseOfLanguageSubdomains()
{
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
var tmp = name + "=;domain=www.domain.com;expires=Thu, 01 Jan 1970 00:00:00 GMT";
console.log(tmp);
document.cookie = tmp;
}
}
deleteOldWwwScopedCookiesThatWereConflictingWithNewNonWwwCookiesThatISwitchedToBecauseOfLanguageSubdomains();
I reload the page nummerous times, and see the console correctly output
data-cookie-name=;domain=www.domain.com;expires=Thu, 01 Jan 1970 00:00:00 GMT
There's no errors in the console.
But if I view the page request in the network tab I get the same cookies listed as above.
I've tried a couple of variations as well:
dropping the domain: data-cookie-name=;expires=Thu, 01 Jan 1970 00:00:00 GMT
adding a path: data-cookie-name=;domain=www.domain.com;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT
No dice.
Nothing I seem to try will get rid of this unwanted cookie that is overriding the .domain.com one.
What am I doing wrong? Please!
I think what I needed was a combination of the two alternatives that I'd tried.
Adding a path AND dropping the domain:
data-cookie-name=;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT
I've no idea why it's important to NOT specify the domain. Can anyone explain?

Set cookie to show div only once per session

I have a div with a Welcome message that shows up before the index page and I want that div to appear only once per session.
How would I set a cookie to show a div only once?
I never used cookies before for something like this, normally I would use Local Storage but this website is visited frequently by older browsers and that's a problem.
You may take a look at the documentation of the document.cookie and more specifically example 3:
if (document.cookie.replace(/(?:(?:^|.*;\s*)someCookieName\s*\=\s*([^;]*).*$)|^.*$/, "$1") !== "true") {
alert("Do something here!");
document.cookie = "someCookieName=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";
}
In this example the cookie is created with expires flag meaning that it will be persistent and survive browser restarts. If you want to perform the action only once per browser session simply remove the expires flag when setting the cookie:
if (document.cookie.replace(/(?:(?:^|.*;\s*)someCookieName\s*\=\s*([^;]*).*$)|^.*$/, "$1") !== "true") {
alert("Do something here!");
document.cookie = "someCookieName=true; path=/";
}

Django: row still displayed after being deleted - because of caching?

I've written a Django app that uses DataTables. The problem is when I delete a row from the table it's still displayed in the table when running against nginx/gunicorn. However, it works correctly when I'm running against the Django test server. So if I start a server with this command line:
python manage.py runserver 192.168.0.1:8000
everything works fine. That is, I delete the row, the table refreshes, and the deleted row is not displayed.
This is a summary of the HTTP calls:
// An initial GET command to populate the table
GET /myapp/get_list (returns a list to display)
// I now select a row and delete it which causes this POST to fire
POST /myapp/delete (deletes a row from the list)
// After the POST the code automatically follows up with a GET to refresh the table
GET /myapp/get_list (returns a list to display)
The problem is when I use nginx/gunicorn the second GET call returns the same list as the first GET including the row that I know has been deleted from the backend database.
I'm not sure it's a caching problem either because this is the response header I get from the first GET:
Date Fri, 23 Dec 2011 15:04:16 GMT
Last-Modified Fri, 23 Dec 2011 15:04:16 GMT
Server nginx/0.7.65
Vary Cookie
Content-Type application/javascript
Cache-Control max-age=0
Expires Fri, 23 Dec 2011 15:04:16 GMT
The problem can be solved also by sending an added parameter to the server so that the browser doesn't cache the call. With jQuery you can simply use:
$.ajaxSetup({ cache: false});
Otherwise you must creat manually the parameter. Usually you create a timestamp
var nocache = new Date().getTime();
//send nocache as a parameter so that the browser thinks it's a new call
You can use Django's add_never_cache_headers or never_cache decorator to tell the browser not to cache given request. Documentation is here. I thinks it's cleaner solution than forcing the cache off in the javascript.
from django.utils.cache import add_never_cache_headers
def your_view(request):
(...)
add_never_cache_headers(response)
return response
from django.views.decorators.cache import never_cache
#never_cache
def your_other_view(request):
(...)
try this
oTable.fnDeleteRow( anSelected, null, true );
and b.t.w what version of datatables do you use?
I fixed the problem by changing
GET /myapp/get_list
to
POST /myapp/get_list
After doing this, I no longer get a cached response.

Cookie doesn't expire on IE6

I am running IE6 from 6.0.3790 (hosted on Citrix Server).
In Logoff we have expired the cookie using
curDate.setTime(curDate.getTime() - 24 * 60 * 60 * 1000);
document.cookie = name + "=; expires=" + curDate.toGMTString() + "; path=/; domain=" + cookieDomain;
where name is the name of cookie.
Problem is with this browser the cookie never expr
Don't use relative date/times for the expiration value. It's safer to use an absolute one like expires=Thu, 01-Jan-1970 00:00:01 GMT;. You're depending on the client's clock to be reasonably accurate, which isn't always the case.
As well, the domain and path settings have to be identical to what they were went the cookie was originally set.
It's also best to set the cookie's value to something obvious, like "deleted", rather than a blank value (name=), in case the browser interprets the lack of a value as "nothing should be changed".