Cookie过期后如何显示内容 [英] How do I display something when a cookie expires

查看:83
本文介绍了Cookie过期后如何显示内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在有人访问该网站且没有Cookie时显示视频.他们看到视频后,会添加一个cookie,但是当cookie过期时,会再次播放视频.



I''m wanting to show a video when a person comes to the site and doesn''t have a cookie. After they see the video, a cookie is added, but when the cookie expires, the video is played again.



if (Request.Cookies["x"] != null)
        {
            //do nothing
        }
        else
        {
            ModalPopupExtender1.Show();
            Response.Cookies["x"].Value = DateTime.Now.ToString();
            Response.Cookies["x"].Expires = DateTime.Now.AddHours(24);
        }



我认为这可以正常工作,但是只有在清除浏览器中的所有Cookie的情况下,电影才会显示. 24小时后,电影将不再播放.



I would think this would work correctly, however the movie only shows if the browser is cleared of all cookies. 24hrs later the movie will not play.

How do I fix this?

推荐答案

Request.Cookies ["x"]将创建cookie(如果不存在).

尝试测试值中是否为空字符串.
Request.Cookies["x"] will create the cookie if it doesn''t exist.

try testing for an empty string in the value instead.


if (Request.Cookies["x"] != null && Convert.ToDateTime(Request.Cookies["x"].Expires) < DateTime.Now)
        {
            //do nothing
        }
        else
        {
            ModalPopupExtender1.Show();
            Response.Cookies["x"].Value = DateTime.Now.ToString();
            Response.Cookies["x"].Expires = DateTime.Now.AddHours(24);
        }



可以.



This works.


http://www.tizag.com/aspTutorial/aspCookie.php
http://www.tizag.com/aspTutorial/aspCookie.php


这篇关于Cookie过期后如何显示内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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