!!!Cookies!!!
-
Hi All, I thought I had finally figured out cookies and have now run into another problem. I have 2 pages which both add data to a cookie as follows **********Page 1********** HttpContext.Current.Response.Cookies["MyCookie"]["Session"]= Session.SessionID; HttpContext.Current.Response.Cookies["MyCookie"]["MyName"] = "Gavin"; HttpContext.Current.Response.Cookies["MyCookie"]["MyDrink"] = "Beer"; **********Page 2********** HttpContext.Current.Response.Cookies["MyCookie"]["MyFood"]= "Pizza"; HttpContext.Current.Response.Cookies["MyCookie"]["MyTeam"] = "Liverpool"; ************************** After Page 1 as I enter Page 2 my 3 Cookie Keys (Session, MyName & MyDrink) are all fine, but as I try and add the 2 new Keys my cookie loses the first 3? I hope everyone understood that one :) Can someone explain why this is and how do I stop it. I am looking at saving about 100 different things in this cookie so I don't really want to rewrite all the keys on every page. Thanks, Gavin
-
Hi All, I thought I had finally figured out cookies and have now run into another problem. I have 2 pages which both add data to a cookie as follows **********Page 1********** HttpContext.Current.Response.Cookies["MyCookie"]["Session"]= Session.SessionID; HttpContext.Current.Response.Cookies["MyCookie"]["MyName"] = "Gavin"; HttpContext.Current.Response.Cookies["MyCookie"]["MyDrink"] = "Beer"; **********Page 2********** HttpContext.Current.Response.Cookies["MyCookie"]["MyFood"]= "Pizza"; HttpContext.Current.Response.Cookies["MyCookie"]["MyTeam"] = "Liverpool"; ************************** After Page 1 as I enter Page 2 my 3 Cookie Keys (Session, MyName & MyDrink) are all fine, but as I try and add the 2 new Keys my cookie loses the first 3? I hope everyone understood that one :) Can someone explain why this is and how do I stop it. I am looking at saving about 100 different things in this cookie so I don't really want to rewrite all the keys on every page. Thanks, Gavin
It appears you're right, the cookies are reset after each round trip; but the more I thought about it the more it made sense. A cookie is just text that is passed back and forth between the server and the client. And if memory serves correctly to not pass the cookie back to the client tells the client to delete the cookie. One thing you can do to preserve the cookies is to loop through the list and re-add the cookie to the response.
for( int i = 0; i < Request.Cookies.Count; i++ ) { Response.Cookies.Add(Request.Cookies[i]); }
This is something I think ASP did for you, but I don't remember off hand. Now, to save you from bickering from your users; PLEASE don't use cookies to store that much data :) Its okay to store a few bits, but you are limited in your cookie space by most browsers; and that is more traffic that goes to the server and back to the client. ASP.NET's session object is now webfarm friendly with use of either an out-of-proc server or using SQL Server as the session state holder. With that said you're going to need a way to store your data, if you can't use a database to store the data then at least put all your data in a memory stream (or some other form so it is one contigous block), compress it, then base64 encode it and store that in the cookie. The space you save will make your users and your provider happy :) Good Luck, James Simplicity Rules! -
It appears you're right, the cookies are reset after each round trip; but the more I thought about it the more it made sense. A cookie is just text that is passed back and forth between the server and the client. And if memory serves correctly to not pass the cookie back to the client tells the client to delete the cookie. One thing you can do to preserve the cookies is to loop through the list and re-add the cookie to the response.
for( int i = 0; i < Request.Cookies.Count; i++ ) { Response.Cookies.Add(Request.Cookies[i]); }
This is something I think ASP did for you, but I don't remember off hand. Now, to save you from bickering from your users; PLEASE don't use cookies to store that much data :) Its okay to store a few bits, but you are limited in your cookie space by most browsers; and that is more traffic that goes to the server and back to the client. ASP.NET's session object is now webfarm friendly with use of either an out-of-proc server or using SQL Server as the session state holder. With that said you're going to need a way to store your data, if you can't use a database to store the data then at least put all your data in a memory stream (or some other form so it is one contigous block), compress it, then base64 encode it and store that in the cookie. The space you save will make your users and your provider happy :) Good Luck, James Simplicity Rules!Okay in original ASp I was told to never store so much data in a Session Object and to either use Cookies or a Database. Now I'm being told not to use Cookies for this much data? You just can't win, at least it makes sense now though, I will use my SQL database instead. Cheers, Gavin PS: Thanks for the help :-D :-D :-D
-
Okay in original ASp I was told to never store so much data in a Session Object and to either use Cookies or a Database. Now I'm being told not to use Cookies for this much data? You just can't win, at least it makes sense now though, I will use my SQL database instead. Cheers, Gavin PS: Thanks for the help :-D :-D :-D
Original ASP had the problem where the session object wasn't webfarm friendly, so you had major scalability issues involved. With ASP.NET you can use a SQL Server Database to store the session data or an out-of-proc server (exe) on the network to host it; so it is webfarm friendly and thus scales pretty well. Now of course session data is going to take up RAM on the server the session is stored on; but how much data is really being taken up per session? Probably not enough to make it matter. James Simplicity Rules!