davidxc davidxc - 1 month ago 17
ASP.NET (C#) Question

How to remember a Session if the user has visited the 'splash/landing' page

I have this really simple C# to redirect users to a splash page when they enter the

/default.aspx
homepage:

if (Session["homepageHoarding"] == null)
{
Response.Redirect("/homepage-hoardings/limited-offer.aspx");
}


On that 'splash' page:
/homepage-hoardings/limited-offer.aspx
, there is a link to return to the 'normal' homepage, which of course is:
/default.aspx


But it redirects again, thinking that the session doesn't exist, which it doesn't.

How do I get it so that once they've seen the Splash page it remembers that they've seen it? I can't use a global because my terrible web-hosts don't allow it :-(

Many thanks :-)

Answer

As said in the comments, use cookies:

bool userVisited = false;
HttpCookie cookie = Request.Cookies["MyCookie"];
if (cookie == null)
{
    cookie = new HttpCookie("MyCookie");
    cookie.Values.Add("userVisitedSplash", true);
    cookie.Expires = DateTime.Now.AddDays(30); //<-- Sets the expiration date
    cookie.HttpOnly = true;
    this.Page.Response.AppendCookie(cookie);
} else{
     userVisited = cookie.Values["userVisitedSplash"]
}
if(userVisited){
    Response.Redirect("~/Default.aspx");
} else{
    Response.Redirect("/homepage-hoardings/limited-offer.aspx");
}