HTML/JavaScript禁用cookie [英] HTML/JavaScript disable cookies

查看:55
本文介绍了HTML/JavaScript禁用cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很高兴认识您.我有问题.

Nice to meet you. I have some problem.

如何通过HTML或JavaScript禁用Cookie?

How to HTML or JavaScript disable cookies?

所有Internet的答案都是将过期时间设置为昨天.就像在代码下一样.

All Internet's answer is expired time set to yesterday. Like under the code.

var date = new Date();
date.setDate(date.getDate() - 1);
var willCookie = '';
willCookie += 'CookieName=Value';
willCookie += 'expires' + date.toUTCString();
document.cookie = willCookie;

如何不以HTML或JavaScript创建cookie?不是浏览器.

How to don't create cookies in HTML or JavaScript? not browser.

推荐答案

如果设置了cookie,它将阻止

It will block if the cookie is being set

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:

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>

这篇关于HTML/JavaScript禁用cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆