Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.



C#
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);
        }


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?
Posted

Request.Cookies["x"] will create the cookie if it doesn't exist.

try testing for an empty string in the value instead.
 
Share this answer
 
C#
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.
 
Share this answer
 
http://www.tizag.com/aspTutorial/aspCookie.php
 
Share this answer
 
Comments
Sandeep Mewara 13-Jul-10 15:22pm    
Reason for my vote of 1
How does this link help OP in his issue?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900