基本的cookie问题

发布于 2024-11-04 13:29:56 字数 160 浏览 1 评论 0原文

对于某个项目,我希望在用户首次访问页面时出现一个警报/通知框,然后假设用户点击“x”关闭通知,它将在后续访问中停止出现 - - 我想这需要设置 cookie(通过 PHP),并且可能包含警报 div 通过 jQuery 响应 cookie 的存在或不存在?非常感谢这里关于如何开始的任何基本指导......

for a certain project I'd like to have an alert/notification box appear upon a user's initial visit to a page, and then assuming the user hits the "x" to close the notification, it will then cease to appear on subsequent visits-- I'd imagine this requires the setting of a cookie (via PHP), and perhaps the containing alert div responding to the presence or absence of the cookie via jQuery? any basic direction here in how to get started is much appreciated...

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

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

发布评论

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

评论(3

叫思念不要吵 2024-11-11 13:29:56

您可以使用浏览器端 cookie 来执行此操作:查看 jquery cookie 插件 http://plugins.jquery.com /项目/Cookie

编辑:这就是我使用它的方式。

保存有用的通知会保留我的 helpfulnotices 对象,因此我不会显示任何超出应显示范围的有用通知。

if(!helpfulnotices.multiupload){
            helpfulnotices.multiupload = true;
            top.flicsy.notify_user(flicsy_lang.message_upload_multiple, "", true);
            self.save_helpful_notices();
        }

下面是使用 jquery JSON cookie 扩展保存结构的代码:

save_helpful_notices: function()
    {
        var date = new Date();
        date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));  // expire in a week or thereabouts
        jQuery.JSONCookie('helpfulnotices', helpfulnotices, {path: '/', expires: date });
    },

在页面加载时我重新水化它:

helpfulnotices = jQuery.JSONCookie('helpfulnotices');

You can do this using browser-side cookies: look at the jquery cookie plugin http://plugins.jquery.com/project/Cookie.

EDIT: here's how I'm using it.

Save helpful notices preserves my helpfulnotices object, so I don't show any one helpful notice more than it should be shown.

if(!helpfulnotices.multiupload){
            helpfulnotices.multiupload = true;
            top.flicsy.notify_user(flicsy_lang.message_upload_multiple, "", true);
            self.save_helpful_notices();
        }

Here's the code to save the structure using the jquery JSON cookie extension:

save_helpful_notices: function()
    {
        var date = new Date();
        date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));  // expire in a week or thereabouts
        jQuery.JSONCookie('helpfulnotices', helpfulnotices, {path: '/', expires: date });
    },

and on page load I re-hydrate it:

helpfulnotices = jQuery.JSONCookie('helpfulnotices');
白首有我共你 2024-11-11 13:29:56

您应该能够使用 cookie 来设置它。但并不能保证用户下次不会看到。它也不能保证新用户会看到它(如果他们在共享计算机上)。

这些事情最好与“用户登录系统”结合使用,这样您就可以单独跟踪每个用户。然后,您可以将设置存储在数据库中,并在后续访问时为每个用户调用该设置。

You should be able to set this with a cookie. However, it will not guarantee that the user will not see it next time. It also won't guarantee that new users will see it (if they are on a shared computer).

These things are always best to do in conjunction with a "user login system" so you can track each user individually. Then you can store the setting in the database and recall the setting for each user on subsequent visits.

岁月无声 2024-11-11 13:29:56

您可以将信息保存在 cookie 中,但这意味着您将始终从客户端到服务器来回发送此 cookie。

如果您想防止这种情况发生,您可以使用名为 localStorage 的新 HTML5 变量

http ://www.bing.com/search?q=html5+localStorage&src=ie9tr

所以基本上,如果浏览器上存在 localStorage,并且不使用 cookies 进行回退,您就可以使用它。这将是最佳解决方案。

You can save the information in a cookie, but that will imply that you will send this cookie back and forth all the time from your client to your server.

If you want to prevent this, you can use the new HTML5 variable called localStorage

http://www.bing.com/search?q=html5+localStorage&src=ie9tr

So basically, you can use localStorage if it exists on the browser, if it doesn't fallback with cookies. That would be the optimal solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文