This code works for server side checking of cookies enabled - but why

A

AAaron123

I have the following:

Sub Session_Start...



Dim gotCookie As Integer

Try

gotCookie = CInt(Request.Cookies("styleSheet").Value)

Session("CookiesEnabled") = True

Catch ex As Exception

gotCookie = 3

Session("CookiesEnabled") = False

End Try

....

without the statements that set the Session variable to indicate if cookies
are supported and enabled. I just added those.

This code is not run each time a page is requested if cookies are supported,
so if the user enables cookies after they've been disabled (and the variable
got set to false) it appears, to me, that the variable will not get set to
true (because this does not run again if cookies are supported).

But I check in page_load and it appears to get set to true even though a
breakpoint shows that the above code is not entered.



Magic?



Can you explain what is going on??

Thanks
 
G

Göran Andersson

AAaron123 said:
I have the following:

Sub Session_Start...



Dim gotCookie As Integer

Try

gotCookie = CInt(Request.Cookies("styleSheet").Value)

Session("CookiesEnabled") = True

Catch ex As Exception

gotCookie = 3

Session("CookiesEnabled") = False

End Try

Don't use Exceptions for normal program execution. Check if the cookie
exists before you try to use it, and use TryParse to parse the value
without causing an exception:

Dim gotCookie As Integer
Dim cookie As HttpCookie = Request.Cookies("styleSheet")
If cookie Is Nothing Then
gotCookie = 3
Else
If not Integer.TryParse(cookie.Value, gotCookie) Then
gotCookie = 3
End If
End If
...

without the statements that set the Session variable to indicate if cookies
are supported and enabled. I just added those.

This code is not run each time a page is requested if cookies are supported,
so if the user enables cookies after they've been disabled (and the variable
got set to false) it appears, to me, that the variable will not get set to
true (because this does not run again if cookies are supported).

Where is the code that keeps the code from running for every request?
 
G

Guest

I have the following:

Sub Session_Start...

Dim gotCookie As Integer

Try

gotCookie = CInt(Request.Cookies("styleSheet").Value)

Session("CookiesEnabled") = True

Catch ex As Exception

gotCookie = 3

Session("CookiesEnabled") = False

End Try

...

without the statements that set the Session variable to indicate if cookies
are supported and enabled. I just added those.

This code is not run each time a page is requested if cookies are supported,
so if the user enables cookies after they've been disabled (and the variable
got set to false) it appears, to me, that the variable will not get set to
true (because this does not run again if cookies are supported).

Hi Aaron,

If session_start is from global.asax then it fires when the session is
started. To determine whether cookies are accepted try to write a
cookie and then try to read it back again. If you cannot read the
cookie you wrote, you assume that cookies are turned off in the
browser.

The following page has an example shows how you might test whether
cookies are accepted.
http://msdn.microsoft.com/en-us/library/ms178194.aspx

Moreover, by default, ASP.NET uses a non-persistent cookie to store
the session state. However, if a user has disabled cookies on the
browser, session state information cannot be stored in a cookie. So,
you code may not work in this case.
 
A

AAaron123

thanks

I have the following:

Sub Session_Start...

Dim gotCookie As Integer

Try

gotCookie = CInt(Request.Cookies("styleSheet").Value)

Session("CookiesEnabled") = True

Catch ex As Exception

gotCookie = 3

Session("CookiesEnabled") = False

End Try

...

without the statements that set the Session variable to indicate if
cookies
are supported and enabled. I just added those.

This code is not run each time a page is requested if cookies are
supported,
so if the user enables cookies after they've been disabled (and the
variable
got set to false) it appears, to me, that the variable will not get set to
true (because this does not run again if cookies are supported).

Hi Aaron,

If session_start is from global.asax then it fires when the session is
started. To determine whether cookies are accepted try to write a
cookie and then try to read it back again. If you cannot read the
cookie you wrote, you assume that cookies are turned off in the
browser.

The following page has an example shows how you might test whether
cookies are accepted.
http://msdn.microsoft.com/en-us/library/ms178194.aspx

Moreover, by default, ASP.NET uses a non-persistent cookie to store
the session state. However, if a user has disabled cookies on the
browser, session state information cannot be stored in a cookie. So,
you code may not work in this case.
 
A

AAaron123

Göran Andersson said:
Don't use Exceptions for normal program execution.

Why? Exceptions are slow? Makes the code hard to understand?
Or what?

Thanks
 
G

Göran Andersson

AAaron123 said:
Why? Exceptions are slow? Makes the code hard to understand?
Or what?

Thanks

Yes, using exceptions is slower. Even if throwing and catching an
exception doesn't take a terrible amount of time (unless you run in
debug mode), checking for a null reference is about 100000 times faster
than handling a NullReferenceException.

If you catch an exception and handle it without caring what kind of
exception it is, you risk hiding some other error in your code. For
example, if your page would be sessionless you could not use the Session
collection, but your code would catch that exception and instead think
that the reason for the exception was that cookies was disabled.

Exceptions are mainly intended for handling exceptional situations, i.e.
when there is some kind of error. Checking for a cookie is a very normal
operation and should not result in an exception.
 
A

AAaron123

Great

Thanks a lot

Göran Andersson said:
Yes, using exceptions is slower. Even if throwing and catching an
exception doesn't take a terrible amount of time (unless you run in debug
mode), checking for a null reference is about 100000 times faster than
handling a NullReferenceException.

If you catch an exception and handle it without caring what kind of
exception it is, you risk hiding some other error in your code. For
example, if your page would be sessionless you could not use the Session
collection, but your code would catch that exception and instead think
that the reason for the exception was that cookies was disabled.

Exceptions are mainly intended for handling exceptional situations, i.e.
when there is some kind of error. Checking for a cookie is a very normal
operation and should not result in an exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top