javascript setCookie expiration wrong or ignored by browser? - javascript

Question about cookie expiration.
I'm trying the setCookie from w3schools.com
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
console.log(expires);
}
when I try (note: today 2020-03-17...)
setCookie("test_cookie", "hello world", 363);
chrome dev says cookie
expires 2020-03-24....
but console.log I added says
expires=Wed, 15 Mar 2021
same thing happens with toGMTString
seems the browser ignores expires, and does 7 days...
so how to really set a cookie expiring in 1 month or 1 year ???

Related

PHP can't get cookie that was set in javascript

I am using PHP 8.0 and this is a wordpress site version 6.1.1
I have a plugin that is setting a cookie like so:
window.tourmaster_set_cookie = function( cname, cvalue, expires ){
if( typeof(expires) != 'undefined' ){
if( expires == 0 ){
expires = 86400;
}
var now = new Date();
var new_time = now.getTime() + (parseInt(expires) * 1000);
now.setTime(new_time);
expires = now.toGMTString();
}
document.cookie = cname + "=" + encodeURIComponent(cvalue) + "; expires=" + expires + "; path=/";
}
This is being called like so:
tourmaster_set_cookie('tourmaster-room-cart', JSON.stringify(cart_cookie), 31536000);
I can see in my console log that this cookie is being set.
However when I try to get the cookie in PHP:
$_COOKIE['tourmaster-room-cart']
I get this error:
Warning: Undefined array key "tourmaster-room-cart"
When I do a print_r on $_COOKIE the cookie "tourmaster-room-cart" is not there.
What is going wrong here and how can I fix it?
UPDATE
I created this simple cookie:
document.cookie = "username=John Doe; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/";
and it appears in my php $_COOKIE;
However this does not show up in $_COOKIE
window.tourmaster_set_cookie = function( cname, cvalue, expires ){
expires = "Thu, 1 Jan 2026 12:00:00 UTC";
if( typeof(expires) != 'undefined' ){
if( expires == 0 ){
expires = 86400;
}
var now = new Date();
var new_time = now.getTime() + (parseInt(expires) * 1000);
now.setTime(new_time);
expires = now.toGMTString();
}
document.cookie = cname + "=test; expires=Thu, 1 Jan 2026 12:00:00 UTC; path=/";
}
ANOTHER UPDATE
I added a few more cookies to see if they would get displayed with $_COOKIES, they all did except for the last test with encodeURIComponent(cvalue) as the value. It must be something in my value? Here is the updated code and below that is the value.
window.tourmaster_set_cookie = function( cname, cvalue, expires ){
if( typeof(expires) != 'undefined' ){
if( expires == 0 ){
expires = 86400;
}
var now = new Date();
var new_time = now.getTime() + (parseInt(expires) * 1000);
now.setTime(new_time);
expires = now.toGMTString();
}
document.cookie = "newusernameagain=John Doe; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/";
document.cookie = "newusername-again=John Doe; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/";
document.cookie = "new-username-again=" + encodeURIComponent(cvalue) + "; expires=Thu, 18 Dec 2025 12:00:00 UTC; path=/";
document.cookie = cname + "=" + encodeURIComponent(cvalue) + "; expires=" + expires + "; path=/";
}
And the value
%5B%7B%22start_date%22%3A%222023-02-05%22%2C%22end_date%22%3A%222023-02-06%22%2C%22room_amount%22%3A%221%22%2C%22adult%22%3A%5B%222%22%5D%2C%22children%22%3A%5B%220%22%5D%2C%22room_id%22%3A%2215701%22%2C%22post_type%22%3A%22room%22%7D%5D
I have no idea what is going wrong.
FINAL UPDATE
For some stupid reason when I remove the dashes from the cookie name and where it is referenced everything works. For some odd reason $_COOKIE does not like dashes for the cookie name on this godaddy server, very odd stuff.
If you have passed a correct value (e.g. "SO test") in cart_cookie, then the script should work.
A normal way to set cookie in JS is like:
document.cookie = "username=John Doe; expires=Thu, 1 Jan 2016 12:00:00 UTC";
So assuming the JS script is:
<script>
window.tourmaster_set_cookie = function( cname, cvalue, expires ){
document.cookie = cname + "=" + encodeURIComponent(cvalue) + "; expires=" + expires + "; path=/";
}
tourmaster_set_cookie('tourmaster-room-cart', JSON.stringify('SO test'), "Thu, 1 Jan 2026 12:00:00 UTC");
alert("Done ! Now redirecting to show the cookie string");
window.location.href="testSO5Feb2023c.php";
</script>
On running it , it will set the cookie, and then redirect to another page (I named it testSO5Feb2023c.php), which shows that the cookie is correctly set
For testSO5Feb2023c.php :
<?php
echo "Cookie is now : <br>";
echo $_COOKIE['tourmaster-room-cart'];
?>
You may see the DEMO
Last but not least, please note that the cookie is retrievable ONLY after it is set, so make sure the page to display the cookie is different from the one you set the cookie, otherwise you may need a page refresh
For some stupid reason when I remove the dashes from the cookie name and where it is referenced everything works. For some odd reason $_COOKIE does not like dashes for the cookie name on this godaddy server, very odd stuff.

delete cookie in JS - why not working without "path=/" in Chrome?

I created cookie by this code:
create_cookie = function(name, value, days){
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
};
create_cookie('ck','123',3); //cookie ck=123, expires after 3 days
When I execute console.log(document.cookie); I can see cookie created successfully.
Is anybody able to answer why this code fail to delete this cookie?
delete_cookie = function(name){
document.cookie = name+'=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};
delete_cookie('ck');
(My browser is Google Chrome 79.0.3945.117 on Mac OSX 10.10)
You are forgetting to provide the path path=/
delete_cookie = function(name){
document.cookie = name+'=;expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/;';
};
delete_cookie('ck');

How to set expiry date in seconds for cookie in JSCookie

I'm using the JSCookie library to save and load cookies.
Now I want to save a cookie with an expiry date. This is officially supported with days like the documentation. But how can I set the expiry in seconds or minutes instead of days?
So I have the following code from the documentation but this is for example only for 7 days:
Cookies.set('name', 'value', { expires: 7, path: '' });
Are there any possibilities to achieve this?
RTFM...
var inFifteenMinutes = new Date(new Date().getTime() + 15 * 60 * 1000);
Cookies.set('foo', 'bar', {expires: inFifteenMinutes})
I've set the cookie the expire in 15 seconds.
function createCookie(name, value) {
var date = new Date();
date.setTime(date.getTime() + (15*1000));
var expires = "; expires= " + date.toGMTString();
document.cookie = name + "=" + value + expires + "; path=/";
}

How to set multiple values with two differents expires dates

I'm trying to set cookie in javascript with two values.
Each of theses have a different expiration date.
For example :
var now = new Date();
now.setDate( now.getDate() + 2 );
document.cookie = "bar=foo;";
document.cookie = "expires=" + now.toUTCString() + ";"
now = new Date();
now.setDate( now.getDate() + 30 );
document.cookie = "foo=bar;";
document.cookie = "expires=" + now.toUTCString() + ";"
Is it correct?
How to set another value with an expiration date for 30 days for example?
I think that approach is correct.
Based on: How can I set a cookie to expire after x days with this code I have? :
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
Note, that setTime() and getTime() work on milliseconds.
And a few words from me: as javascript's Date sucks, I recommend using moment.js library when working with dates, it's brilliant.
Ok, i found my reply here with the function "setCookie". I've specify differents values and it's working.
http://www.w3schools.com/js/js_cookies.asp

Cookie set using javascript disappears in IE 10 / IE 8 after some time

//http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
}
document.cookie = name+"="+value+expires+"; path=/";
}
I am trying to create cookie using the above function, on page load. And trying to observe cookie value using the below code in IE 10's the javascript console.
function setCookieAndTrack () {
createCookie ('test', 'someValue', 99);
var test = 0;
setInterval(function () {
console.log('document.cookie -- ', document.cookie, " - ", test++, " - ", (new Date()).getTime());
}, 1000);
}
And then on body load calling the above function
<body onload="setCookieAndTrack();">
Here is the response I get when I am starting IE 10 fresh without any cached files.
document.cookie -- test=someValue - 0 - 1376957958770
document.cookie -- test=someValue - 1 - 1376957959753
document.cookie -- test=someValue - 2 - 1376957960751
document.cookie -- test=someValue - 3 - 1376957961749
document.cookie -- test=someValue - 4 - 1376957962747
document.cookie -- test=someValue - 5 - 1376957963746
document.cookie -- test=someValue - 6 - 1376957964760
document.cookie -- - 7 - 1376957965744
document.cookie -- - 8 - 1376957966757
document.cookie -- - 9 - 1376957967755
document.cookie -- - 10 - 1376957968769
document.cookie -- - 11 - 1376957969752
document.cookie -- - 12 - 1376957970766
document.cookie -- - 13 - 1376957971764
document.cookie -- - 14 - 1376957972747
document.cookie -- - 15 - 1376957973761
Thing is, this only happens when the browser starts fresh. After refreshing page, cookie value does not vanish.
I am using IE 10 (10.0.9200.16660) on Windows 7 (64-bit). I have also observed this with other IE versions.
Am I missing anything here?
Give this a go - try encoding the value and using UTCString instead. According to MDN, toUTCString should be used: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
It actually says GMT string in the docs, but then directs you to UTC.
function createCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toUTCString();
}
document.cookie = name+"="+encodeURIComponent(value)+expires+"; path=/";
}

Categories

Resources