HTML/JavaScript 禁用 cookie [关闭]
Posted
技术标签:
【中文标题】HTML/JavaScript 禁用 cookie [关闭]【英文标题】:HTML/JavaScript disable cookies [closed] 【发布时间】:2017-05-27 03:22:45 【问题描述】:很高兴认识你。 我有一些问题。
如何通过 html 或 javascript 禁用 cookie?
所有互联网的回答都是过期时间设置为昨天。 就像在代码下一样。
var date = new Date();
date.setDate(date.getDate() - 1);
var willCookie = '';
willCookie += 'CookieName=Value';
willCookie += 'expires' + date.toUTCString();
document.cookie = willCookie;
如何不在 HTML 或 JavaScript 中创建 cookie?不是浏览器。
【问题讨论】:
您是否要为您的网站禁用 cookie 功能..,? 【参考方案1】:如果设置了 cookie 会阻塞
if(!document.__defineGetter__)
Object.defineProperty(document, 'cookie',
get: function()return '',
set: function()return true,
);
else
document.__defineGetter__("cookie", function() return ''; );
document.__defineSetter__("cookie", function() );
对于 php:
$dirty = false;
foreach(headers_list() as $header)
if($dirty) continue; // I already know it needs to be cleaned
if(preg_match('/Set-Cookie/',$header)) $dirty = true;
if($dirty)
$phpversion = explode('.',phpversion());
if($phpversion[1] >= 3)
header_remove('Set-Cookie'); // php 5.3
else
header('Set-Cookie:'); // php 5.2
Javascript:
<script type="text/javascript">
// remember, these are the possible parameters for Set_Cookie:
// name, value, expires, path, domain, secure
Set_Cookie( 'test', 'none', '', '/', '', '' );
// if Get_Cookie succeeds, cookies are enabled, since
//the cookie was successfully created.
if ( Get_Cookie( 'test' ) )
document.write( 'cookies are currently enabled.' );
/*
this is an example of a set cookie variable, if
you want to use this on the page or on another script
instead of writing to the page you would just check that value
for true or false and then do what you need to do.
*/
cookie_set = true;
// and these are the parameters for Delete_Cookie:
// name, path, domain
// make sure you use the same parameters in Set and Delete Cookie.
Delete_Cookie('test', '/', '');
// if the Get_Cookie test fails, cookies are not enabled for this session.
else
document.write('cookies are not currently enabled.');
cookie_set = false;
</script>
【讨论】:
至少在 chrome 上,如果我启用 cookieSetter 行,它仍然会禁用 cookieGetter...【参考方案2】:试试下面的代码:
cookieExpire = new Date();
cookieExpire.setMonth(cookieExpire.getMonth() + 10);
还请检查此链接: Is it possible to block cookies from being set using Javascript or PHP?
【讨论】:
以上是关于HTML/JavaScript 禁用 cookie [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
Charles——charles 工具栏Tools总结——禁用cookies/Block Cookies Settings