使用 jQuery Cookie 插件是否是测试 cookie 是否已启用的有效方法?

发布于 2024-08-16 22:26:12 字数 331 浏览 6 评论 0原文

我有一个网站,我们要求用户在登录该网站之前启用 JavaScript 和 cookie。 (JS部分已经完成并且工作完美。)目前,我们一直在设置cookie,然后将用户重定向到另一个页面(在PHP中)。这工作得很好,但现在我们有一群人为我们重定向到的页面添加了书签,当然该页面没有设置 cookie,因此不允许他们登录。

所以我试图找到另一个解决方案来检查 cookie,我正在考虑使用 jQuery Cookie 插件。我想知道它是否在所有浏览器中兼容(当然是在启用 JS 的情况下)?

I have a site that we require the user to have enabled JavaScript and cookies before they can login to the site. (The JS part is done and works perfectly.) At the moment, we have been been setting a cookie and then redirect the user to another page (in PHP). This has worked fine, but now we have a bunch of people that have bookmarked the page we are redirecting to, which of course doesn't have the cookie set and therefore doesn't allow them to login.

So I'm trying to find another solution to check for the cookie and I'm thinking of using the jQuery Cookie plugin. I'm wondering if it's compatible in all browsers (when JS is enabled of course)?

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

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

发布评论

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

评论(3

╭ゆ眷念 2024-08-23 22:26:12

谢谢肖恩 您的答案,但不幸的是,由于浏览器并不总是发送引荐来源网址,因此它不够可靠,无法每次都使用它。因为如果没有设置,那么你就会陷入循环。

我想到的另一个解决方案是重定向到一个完全独立的页面,该页面本身会检查 cookie 是否已启用(通过重定向到自身)。如果启用cookie,它将重定向回原始页面。如果不是,那么它将重定向到有关该问题的页面。我认为这应该有效,但我不确定。

最后,我在 IE 6、7 和 8、Safari 4、Google Chrome 4、Firefox 3.5、Opera 10.2 以及一些不同的配置中尝试了 jQuery Cooke 插件,它在所有这些配置中都有效。这是我正在使用的代码:

$.cookie('test_cookie', 'cookie_value', { path: '/' });
if ($.cookie('test_cookie') == 'cookie_value') {
    // cookie worked, set/enable appropriate things
}

它并不完美,但我认为它可以在 95% 的情况下工作。否则,它就会失败,并且不允许他们做任何事情。

Thxs Shawn for your answer, but unfortunately because browsers don't always send the referrer, it isn't reliable enough to be able to use it every time. Because if it isn't set, then you kind of end up in a loop.

The one other solution that I thought of was to redirect to a completely separate page, which in itself checks to see if cookies are enabled (by redirecting to itself). If cookies are enabled, it would redirect back to the original page. If they are not, then it would redirect to a page about the problem. I think that should work, but I'm not sure.

In the end, I tried the jQuery Cooke plugin in IE 6, 7, and 8, Safari 4, Google Chrome 4, Firefox 3.5, Opera 10.2 and on a few different configurations and it worked in all of them. Here is the code I'm using:

$.cookie('test_cookie', 'cookie_value', { path: '/' });
if ($.cookie('test_cookie') == 'cookie_value') {
    // cookie worked, set/enable appropriate things
}

It's not perfect, but I'm thinking it will work in 95% of cases. Otherwise, it will fail and just not allow them to do anything.

梦里°也失望 2024-08-23 22:26:12

您可以检查页面的引荐来源网址,如果它不是您的主页,您可以将其重定向到那里。如果引荐来源网址是您的主页并且没有设置 cookie,您就会知道他们没有启用 cookie。

You could check the referrer of the page and if it's not your home page, you can redirect them there. If the referrer is your home page and there's no cookie set, you'll know they do not have cookies enabled.

铜锣湾横着走 2024-08-23 22:26:12

我喜欢这个 1 衬垫函数:

function cookiesEnabled() {
    return $.cookie('check', 'valid', { expires: 1 }) && $.cookie('check') == 'valid';
}

I like this 1 liner function:

function cookiesEnabled() {
    return $.cookie('check', 'valid', { expires: 1 }) && $.cookie('check') == 'valid';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文