是否可以使用 Modernizr 检查 Cookie 是否已启用?

发布于 2025-01-02 09:34:30 字数 291 浏览 5 评论 0原文

我正在研究如何检查浏览器中是否启用了 cookie,我找到了很多答案,我什至测试了一些答案,但之后我的一个朋友建议我使用 Modernizr 为此。
我开始搜索,发现很多与 CSS3HTML5 相关的东西,但我不想要那样,我只是想知道是否有可能检查Modernizr是否启用了cookie

I was researching about how to check if the cookies are enabled in a browser and i found a lot of answer, i even tested a few ones, but after that a friend of mine suggest me to use Modernizr for that.
I started to search about that and i found a lot of stuff related with CSS3 and HTML5, but i don't want that, i just wanna know if is it possible to check that cookies are enabled or not with Modernizr?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情话墙 2025-01-09 09:34:30

下面的代码是从 http://sveinbjorn.org/cookiecheck 复制的。

function are_cookies_enabled()
{
    var cookieEnabled = (navigator.cookieEnabled) ? true : false;

    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
    { 
        document.cookie="testcookie";
        cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }
    return (cookieEnabled);
}

Below code is copied from http://sveinbjorn.org/cookiecheck.

function are_cookies_enabled()
{
    var cookieEnabled = (navigator.cookieEnabled) ? true : false;

    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
    { 
        document.cookie="testcookie";
        cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }
    return (cookieEnabled);
}
向日葵 2025-01-09 09:34:30

这个问题的直接答案是“是的!”它内置于

示例代码中:

if (Modernizr.cookies == false) {

    alert('Please enable cookies');    
}
else { 
    // do something with cookies
}

您还可以使用 css 类 .cookies.no-cookies 来显示/隐藏面板,告诉用户他们需要启用 cookie。

.cookies #noCookies
{
    display: none;
}

<div id='#noCookies'>
   This site requires cookies! Please turn them on already!
</div>

(这个 .cookies 类由 Modernizr 添加到 标记中)。

注意:如果您正在创建 Modernizr 的自定义版本,则 cookies 选项当前“隐藏”在“非核心检测”部分下。

A direct answer to the question is 'Yes!' and it is built in

Example code:

if (Modernizr.cookies == false) {

    alert('Please enable cookies');    
}
else { 
    // do something with cookies
}

You can also use the css class .cookies or .no-cookies to show/hide a panel telling the user they need cookies enabled.

.cookies #noCookies
{
    display: none;
}

<div id='#noCookies'>
   This site requires cookies! Please turn them on already!
</div>

(This .cookies class is added to <body> tag by Modernizr).

Note: If you are creating a custom build of Modernizr the cookies option is currently 'hidden' under the 'Non-core detects' section.

维持三分热 2025-01-09 09:34:30

PHP

HTML/PHP 的另一种方式:

<?php
  session_start();
  $_SESSION['cook'] = 1;
  echo "<img src=\"cookcheck.php\">";
?>

PHP - cookcheck.php:

 <?php
     session_start();

     if ($_SESSION['cook'] !== 1) 
                             { $image="/nocookmsg.png"; }    # Cookies NOT Enabled
                               else { $image="/blank.png"; } # Cookies Enabled

     $img=imageCreateFromPNG($image);   # Create Image
     header("Content-type: image/png"); # Send Header
     imagePNG($image);                  # Send Image
 ?>

Another way with PHP

HTML/PHP:

<?php
  session_start();
  $_SESSION['cook'] = 1;
  echo "<img src=\"cookcheck.php\">";
?>

PHP - cookcheck.php:

 <?php
     session_start();

     if ($_SESSION['cook'] !== 1) 
                             { $image="/nocookmsg.png"; }    # Cookies NOT Enabled
                               else { $image="/blank.png"; } # Cookies Enabled

     $img=imageCreateFromPNG($image);   # Create Image
     header("Content-type: image/png"); # Send Header
     imagePNG($image);                  # Send Image
 ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文