Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete cookie on codeigniter

I don't know how to delete a cookie. I want is when I submit a form. The cookie is also delete. I try the delete_cookie("name") but is not working. I think because the cookie I created by javascript. Please check my code to fix this problem.

This is my sample text field:

<input name="cargo_no" type="text"  class="validate[required]"  id="cargonumber" onchange="setCookie('cargonumberC', this.value, 365);"/>

and this is the javascript

function setCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();

    if (!nDays) 
        nDays=1;

    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

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

document.addEventListener('DOMContentLoaded', function() {
  var themeSelect = document.getElementById('cargonumber');
  var selectedTheme = readCookie('cargonumberC');

  if (selectedTheme) {
    themeSelect.value = selectedTheme;
  }
});
like image 496
wewe Avatar asked Sep 02 '13 03:09

wewe


People also ask

What is Set_Cookie () function in CodeIgniter?

Cookie is a small piece of data sent from web server to store on client’s computer. CodeIgniter has one helper called “Cookie Helper” for cookie management. In the set_cookie () function, we can pass all the values using two ways. In the first way, only array can be passed and in the second way, individual parameters can also be passed.

How to get and delete a cookie in Cookie_controller?

The get_cookie () function is used to get the cookie that has been set using the set_cookie () function. The delete_cookie () function is used to delete the cookie (). Create a controller called Cookie_controller.php and save it at application/controller/Cookie_controller.php

What is the default cookie type for the SameSite?

This default is Lax. If you have set the SameSite to be an empty string and your default SameSite is also an empty string, your cookie will be given the Lax value. If the SameSite is set to None you need to make sure that Secure is also set to true.

What is an HTTP cookie?

An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to the user’s web browser. The browser may store it and send it back with later requests to the same server. Typically, it’s used to tell if two requests came from the same browser - keeping a user logged-in, for example.


1 Answers

Using Codeigniter, put it inside the save method of your controller:

Try:

delete_cookie('name', $domain, $path); 

Details on official documentation

like image 80
Nivlem Castro Avatar answered Nov 15 '22 20:11

Nivlem Castro