javascript cookies are not deleting - javascript

i write a small javascript function
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}
function deleteCookie(key)
{
// Delete a cookie by setting the date of expiry to yesterday
date = new Date();
date.setDate(date.getDate() -1);
document.cookie = escape(key) + '=;expires=' + date;
}
in my console .i set a cookie by document.cookie = "Next=true";
i called getCookie('Next') .its returning true
i called deleteCookie('Next') and then called getCookie('Next') still its returning true.can anyone please tell why its not deleting cookies ??

Try this one:
function deleteCookie(key) {
document.cookie =
encodeURIComponent(key) +
"=deleted; expires=" +
new Date(0).toUTCString();
}

To delete a cookie with JQuery
//To set a cookie
02
$.cookie('the_cookie', 'the_value');
03
04
//Create expiring cookie, 7 days from then:
05
$.cookie('the_cookie', 'the_value', { expires: 7 });
06
07
//Create expiring cookie, valid across entire page:
08
$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
09
10
//Read cookie
11
$.cookie('the_cookie'); // => 'the_value'
12
$.cookie('not_existing'); // => null
13
14
//Delete cookie by passing null as value:
15
$.cookie('the_cookie', null);
16
17
// Creating cookie with all availabl options
18
$.cookie('myCookie2', 'myValue2', { expires: 7, path: '/', domain: 'example.com',
19
secure: true, raw: true });

Also setting the cookie's domain worked for me:
document.cookie = 'cookiename=; Max-Age=0; path=/; domain=.example.com';

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

javascript: deleting cookie not working as expected

Here's my code:
delete_cookie("intro");
var intro = readCookie("intro");
alert(intro);
function readCookie(name)
{
var nameEQ = encodeURIComponent(name) + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
}
return null;
}
function delete_cookie( name )
{
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
For some reason, it is not working. When reading the cookie it still returns 1.
Does anyone know what's wrong?
You should define path on which cookie exists to ensure that you delete the real one
Try using
function delete_cookie(name) {
document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
Try including the domain like so: document.cookie = name + "=; path=/; domain=.MYDOMAIN"; expires=Thu, 01 Jan 1970 00:00:01 GMT;";
I had the same problem some time ago, and for some reason it seemed to be trying to access the cookie on the wrong domain. Also make sure when you set the cookie, to use the same exact domain.

Javascript to delete JSESSIONID cookie not working

I am having weird issue with IE10. IE10 is sending expired JSESSIONID cookie for authentication causing the login filure, So am trying to delete the JSESSIONID cookie as below
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) {
return c.substring(name.length, c.length);
}
}
return "";
}
if(getCookie("JSESSIONID"))
{
var c = getCookie("JSESSIONID")
console.log("JSESSIONID = "+ c)
document.cookie = c + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
Whenever I reload the page I see JSESSIONID = A64F97BF3AF662AC56238F2C23D529AA in the console log
instead of JSESSIONID = A64F97BF3AF662AC56238F2C23D529AA=; expires=Thu, 01 Jan 1970 00:00:01 GMT;
Can someone please help me to fix this issue?
You're keeping the old cookie value and adding = to the end of it. You should be setting the value to an empty string:
document.cookie = 'JSESSIONID=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

How can I delete all cookies with JavaScript? [duplicate]

This question already has answers here:
Clearing all cookies with JavaScript
(26 answers)
Closed 6 years ago.
I have written code to save cookies in JavaScript. Now I need to clear the cookies irrespective of values that I assigned.
Are there any script modules to delete all cookies that were generated by Javascript?
My Sample Code:
document.cookie = 'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'
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=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
How else could I clear all of the cookies?
Will there will be any problems when I test the code on the webserver?
There is no 100% solution to delete browser cookies.
The problem is that cookies are uniquely identified by not just by their key "name" but also their "domain" and "path".
Without knowing the "domain" and "path" of a cookie, you cannot reliably delete it. This information is not available through JavaScript's document.cookie. It's not available through the HTTP Cookie header either!
However, if you know the name, path and domain of a cookie, then you can clear it by setting an empty cookie with an expiry date in the past, for example:
function clearCookie(name, domain, path){
var domain = domain || document.domain;
var path = path || "/";
document.cookie = name + "=; expires=" + +new Date + "; domain=" + domain + "; path=" + path;
};
On the face of it, it looks okay - if you call eraseCookie() on each cookie that is read from document.cookie, then all of your cookies will be gone.
Try this:
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
eraseCookie(cookies[i].split("=")[0]);
All of this with the following caveat:
JavaScript cannot remove cookies that have the HttpOnly flag set.
This is a function we are using in our application and it is working fine.
delete cookie: No argument method
function clearListCookies()
{
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
{
var spcook = cookies[i].split("=");
deleteCookie(spcook[0]);
}
function deleteCookie(cookiename)
{
var d = new Date();
d.setDate(d.getDate() - 1);
var expires = ";expires="+d;
var name=cookiename;
//alert(name);
var value="";
document.cookie = name + "=" + value + expires + "; path=/acc/html";
}
window.location = ""; // TO REFRESH THE PAGE
}
Edit: This will delete the cookie by setting it to yesterday's date.
Why do you use new Date instead of a static UTC string?
function clearListCookies(){
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++){
var spcook = cookies[i].split("=");
document.cookie = spcook[0] + "=;expires=Thu, 21 Sep 1979 00:00:01 UTC;";
}
}

Categories

Resources