How to delete cookie on codeigniter

依然范特西╮ 提交于 2021-02-07 12:19:59

问题


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

回答1:


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

Try:

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

Details on official documentation




回答2:


You can delete cookie from CodeIgniter. Use cookie helper like

$this->load->helper('cookie');
delete_cookie("name");



回答3:


You can't delete a cookie. The browser (or the user) has the delete the cookie(s). But, you can make the browser auto-remove the cookie by setting the expiration of the cookie to a date in the past. Here's a JavaScript example.

function deleteCookie(cookieName, cookieValue) {

document.cookie = cookieName+"="+escape(cookieValue) + ";expires=Thu, 01 Jan 1970 00:00:01 GMT;";

}


来源:https://stackoverflow.com/questions/18565336/how-to-delete-cookie-on-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!