Forum Moderators: open

Message Too Old, No Replies

Need help - checking for cookie on page load with redirect

Newbie needs coding help

         

jdkuehne

9:20 pm on Aug 1, 2008 (gmt 0)

10+ Year Member



I thought I could figure this out, but so far no joy.

Working in ASP.NET 2.0 VB, I need to check for the existence of a cookie on entering a page and if it exists, immediately redirect to another page. This is a validation portal and further down on the page, I am also checking for the value of the cookie (in the page behind code) to see if it has been set, and if not, then setting it(this part is working). In other words, the first time a user hits this page, he has has to check a box before being directed to his destination page. On subsequent visits during the same browser session, the user is sent directly to the destination page.

How can I check for this cookie without loading the page? Do I put a script in the <head> of the page? And what would the code look like. I need some guidance here. Thanks.

jdkuehne

9:17 pm on Aug 6, 2008 (gmt 0)

10+ Year Member



Can no one help with this?

Ocean10000

2:53 am on Aug 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This code is just quick and dirty off the top of my head how to do it.

protected void Page_Load(object sender, EventArgs e)
{
if (this.Request.Cookies["cookiename"] != null && this.Request.Cookies["cookiename"].Value == "Expectedvalue")
{
this.Response.Redirect("URL TO REDIREC TO");
}
}

[edited by: Ocean10000 at 2:55 am (utc) on Aug. 8, 2008]

jdkuehne

9:19 pm on Aug 8, 2008 (gmt 0)

10+ Year Member



Thanks for the reply Ocean10000. This looks promising. I will give it a try.

jdkuehne

8:42 pm on Aug 13, 2008 (gmt 0)

10+ Year Member



I've almost got it, I think. After translating Ocean10000's code into VB, the code behind page now reads:

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim pass As String
pass = Request.QueryString("new")
If ((Not (Me.Request.Cookies("visited")) Is Nothing) _
AndAlso (Me.Request.Cookies("visited").Value = "true")) Then
Me.Response.Redirect(pass)
End If
End Sub

Protected Sub SubmitFirstDemo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
Dim pass As String
pass = Request.QueryString("new")
If (pass = "") Then
Response.Redirect("http://www.rssd.com")
End If
If Page.IsValid() Then
Response.Cookies("visited").Value = "true"
Response.Cookies("visited").Expires = DateTime.MinValue
Response.Redirect(pass)
End If
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class


This is throwing an error because Page_Load has multiple definitions and I am not sure how to rewrite this correctly. Also, I've seen various arguments for the best way to ensure the cookie expires with the session. Is the DateTime.MinValue method a good way of doing this, or is there a better way? I would appreciate any help or comments. Thanks.

Ocean10000

3:52 am on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try the following. I removed the second Page_load that was at the end.

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Load
Dim pass As String
pass = Request.QueryString("new")
If ((Not (Me.Request.Cookies("visited")) Is Nothing) _
AndAlso (Me.Request.Cookies("visited").Value = "true")) Then
Me.Response.Redirect(pass)
End If
End Sub

Protected Sub SubmitFirstDemo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
Dim pass As String
pass = Request.QueryString("new")
If (pass = "") Then
Response.Redirect("http://www.rssd.com")
End If
If Page.IsValid() Then
Response.Cookies("visited").Value = "true"
Response.Cookies("visited").Expires = DateTime.MinValue
Response.Redirect(pass)
End If
End Sub
End Class

jdkuehne

8:54 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Thanks Ocean10000! I think that's done the trick (after adding the If (pass = '') clause to the first subsection)


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim pass As String
pass = Request.QueryString("new")
If (pass = "") Then
Response.Redirect("http://www.home_page.com")
End If
If ((Not (Me.Request.Cookies("visited")) Is Nothing) _
AndAlso (Me.Request.Cookies("visited").Value = "true")) Then
Me.Response.Redirect(pass)
End If
End Sub

Protected Sub SubmitFirstDemo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
Dim pass As String
pass = Request.QueryString("new")
If (pass = "") Then
Response.Redirect("http://www.home_page.com")
End If
If Page.IsValid() Then
Response.Cookies("visited").Value = "true"
Response.Cookies("visited").Expires = DateTime.MinValue
Response.Redirect(pass)
End If
End Sub
End Class

Now to check it on the live server....

jdkuehne

9:08 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Well, I guess I got happy too soon. The above code has a logic error somewhere. It is not sending the user to the validation page and not setting the cookie. I will study it to see if I can figure it out, but I would appreciate any thoughts.

jdkuehne

7:38 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



Actually, I now believe the above solution is working okay. I was testing in Firefox and was expecting a session to be the life of the tabbed window, but evidently, it is the entire browser instance. After closing Firefox completely and retrying, and also testing in other browsers, it seems to be functioning correctly.

Now the next question is how do I integrate the development web.config file with the live one. I think I had better start a new thread. Thanks to all who helped.