Code Listing: Create, read, store information in a cookie
private void Page_Load(object sender, System.EventArgs e)
{
// Check if Cookies are accepted
if(Request.Browser.Cookie)
{
if(Request.Cookies[“LastVisit”]==null)
{
// Create the Cookie
HttpCookie cookLastVisit = new HttpCookie(“LastVisit”,DateTime.Now.ToString());
// Set the expiration
cookLastVisit.Expires = DateTime.Now.AddDays(1);
// Add to Cookies collection
Response.Cookies.Add(cookLastVisit);
// Display message
lblMessage.Text = “This is your first visit”;
}
else
{
// Get the Cookie
HttpCookie cookLastVisit = Request.Cookies[“LastVisit”];
lblMessage.Text = “You last visited this page on: ” + cookLastVisit.Value;
Response.Cookies[“LastVisit”].Value = DateTime.Now.ToString();
Response.Cookies[“LastVisit”].Expires = DateTime.Now.AddDays(1);
}
}
else { lblMessage.Text = “Your browser does not accept cookies”; }
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment