Checking to see if a cookie exists
-
Hi All, Quick question here, I need to check to see if a cookie exists when someone enters 1 of my pages. This is an intranet so I know that everyone accepts cookies but is there way to see if a certain cookie exists? :~ :~ :~ Thanks, Gavin
-
Hi All, Quick question here, I need to check to see if a cookie exists when someone enters 1 of my pages. This is an intranet so I know that everyone accepts cookies but is there way to see if a certain cookie exists? :~ :~ :~ Thanks, Gavin
Check the cookie for
null
:) James Simplicity Rules! -
Check the cookie for
null
:) James Simplicity Rules!Tried that, for some reason the page falls over if I try the following if (HttpContext.Current.Request.Cookies["NewOutbreak"]["Test"] == "") { //Do Something amazing } else { Go home } I get an error saying something like cannot check for null as it doesn't exist.??
-
Tried that, for some reason the page falls over if I try the following if (HttpContext.Current.Request.Cookies["NewOutbreak"]["Test"] == "") { //Do Something amazing } else { Go home } I get an error saying something like cannot check for null as it doesn't exist.??
I wonder if you are getting the error from the "NewOutbreak" cookie not existing and then trying to access it. Give this a shot...
Request r = HttpContext.Current.Request; if(r.Cookies["NewOutbreak"] == null) { // NewOutbreak cookie is not set } else if( r.Cookies["NewOutbreak"]["Test"] == null) { // NewOutbreak.Test cookie is not set } else { // NewOutbreak.Test cookie IS set }
I'm still working out your issue at the top :) James Simplicity Rules! -
I wonder if you are getting the error from the "NewOutbreak" cookie not existing and then trying to access it. Give this a shot...
Request r = HttpContext.Current.Request; if(r.Cookies["NewOutbreak"] == null) { // NewOutbreak cookie is not set } else if( r.Cookies["NewOutbreak"]["Test"] == null) { // NewOutbreak.Test cookie is not set } else { // NewOutbreak.Test cookie IS set }
I'm still working out your issue at the top :) James Simplicity Rules!:cool: werkin :cool: I see the logic, you have to check to see if the Cookie exists before checking to see if the cookies key exists or has any data... Seems to be 100% Thanks :)