php 如何删除codeigniter上的cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18565336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete cookie on codeigniter
提问by wewe
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.
我不知道如何删除 cookie。我想要的是当我提交表单时。cookie 也被删除。我尝试了 delete_cookie("name") 但不起作用。我想是因为我创建的 cookie javascript
。请检查我的代码以解决此问题。
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
这是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;
}
});
回答by Nivlem Castro
Using Codeigniter, put it inside the save method of your controller:
使用 Codeigniter,将其放入控制器的 save 方法中:
Try:
尝试:
delete_cookie('name', $domain, $path);
Details on official documentation
官方文档的详细信息
回答by SarwarCSE
You can delete cookie from CodeIgniter. Use cookie helper like
您可以从 CodeIgniter 中删除 cookie。使用 cookie helper 之类的
$this->load->helper('cookie');
delete_cookie("name");
回答by user2738331
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.
您无法删除 cookie。浏览器(或用户)有权删除 cookie。但是,您可以通过将 cookie 的过期时间设置为过去的日期来让浏览器自动删除 cookie。这是一个 JavaScript 示例。
function deleteCookie(cookieName, cookieValue) {
document.cookie = cookieName+"="+escape(cookieValue) + ";expires=Thu, 01 Jan 1970 00:00:01 GMT;";
}