473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Intermittent loss of session variables and cookie contents

My application (relevant code snippets below) originally used Session
variables in order to maintain state from page to page. After being
unable to solve the mystery of why those variables were intermittently
inaccessible, (all Session variables gone, not using InProc Session
state mode), I moved to a cookies-based solution. Now I have cookie
contents that are intermittently inaccessible. Someone suggested using
session variables to "back-up" the cookie functionality, so if one was
not available, I could fall back on the other. Now it turns out that
when one is missing, the other is missing as well.

This is a simple retrieval (from a MSSQL DB) and storage (in a cookie
and session variable at the same time) of a Customer ID upon
successful logon. Then, when customer info in needed, an attempt is
made to get the necessary Customer ID from the cookie. If that is
null, an attempt is made to get it from the Session variable. If that
is null, a 0 value is used for the Cust ID, eventually resulting in an
empty Datareader object.

We've done tons of Google searches on "disappearing cookies"
"disappearing session variables", etc. Until today, when we came up
with the idea of using Session variables to "backup" cookie contents,
we were not aware that both "cookies disappearing" and "Session
variables disappearing" were happening at the same time.

Let me emaphisize that this is only happening about a third of the
time. I have not been able to replicate either the "disappearing
cookie" and disappearing Session variable" behavior myself. I have an
error-trapping routine that emails me with the stack trace upon the
first invalid read of the empty datareader. Also, for what it's worth,
Verio is the hosting company involved.

CODE
_____
Cookie and session variable setting in Logon page:

Dim CustInfo As CWI.CustomersDB = New CWI.CustomersDB()
Dim rdr As SqlDataReader

rdr = CustInfo.CustomerLogin(tbxEmail.Text, tbxPassword.Text)

If Not rdr.Read() Then
lblLogonFailed.Text = "No such Email/Password"

Else
Session.Add("CustID", rdr.GetValue(0))
Dim cookCustID As New HttpCookie("CID")
cookCustID.Values.Add("CustID", CStr(rdr.GetValue(0)))
Dim dtNow As New DateTime()
Dim tsMinutes As New TimeSpan(0, 2, 0, 0)
dtNow = Now.Add(tsMinutes)
cookCustID.Expires = dtNow
Response.Cookies.Add(cookCustID)
End If
COOKIE & SESSION Retrival logic (standard read logic at end omitted)

Dim CustID As Integer
Dim cookCustID As HttpCookie
Dim context As HttpContext = HttpContext.Current

cookCustID = context.Request.Cookies("CID")

If cookCustID Is Nothing Then
If IsDBNull(context.Session.Item("CustID")) Then
CustID = 0
Else
CustID = CInt(context.Session.Item("CustID"))
End If
Else
CustID = cookCustID.Values.Item("CustID")
End If
Nov 18 '05 #1
1 3150
Hi Steve,

You've probably checked, but make sure you don't have anti-virus software
scanning your Webs or the Temporary files location. They've been known to
make ASP.NET think that a file has changed, causing it to reset the whole
application - taking the Sessions with it. Worse, some virus scanners detect
changes to files, so they scan them again and the vicious circle continues.

"Steve Remer" <sd***@msn.com> wrote in message
news:34**************************@posting.google.c om...
My application (relevant code snippets below) originally used Session
variables in order to maintain state from page to page. After being
unable to solve the mystery of why those variables were intermittently
inaccessible, (all Session variables gone, not using InProc Session
state mode), I moved to a cookies-based solution. Now I have cookie
contents that are intermittently inaccessible. Someone suggested using
session variables to "back-up" the cookie functionality, so if one was
not available, I could fall back on the other. Now it turns out that
when one is missing, the other is missing as well.

This is a simple retrieval (from a MSSQL DB) and storage (in a cookie
and session variable at the same time) of a Customer ID upon
successful logon. Then, when customer info in needed, an attempt is
made to get the necessary Customer ID from the cookie. If that is
null, an attempt is made to get it from the Session variable. If that
is null, a 0 value is used for the Cust ID, eventually resulting in an
empty Datareader object.

We've done tons of Google searches on "disappearing cookies"
"disappearing session variables", etc. Until today, when we came up
with the idea of using Session variables to "backup" cookie contents,
we were not aware that both "cookies disappearing" and "Session
variables disappearing" were happening at the same time.

Let me emaphisize that this is only happening about a third of the
time. I have not been able to replicate either the "disappearing
cookie" and disappearing Session variable" behavior myself. I have an
error-trapping routine that emails me with the stack trace upon the
first invalid read of the empty datareader. Also, for what it's worth,
Verio is the hosting company involved.

CODE
_____
Cookie and session variable setting in Logon page:

Dim CustInfo As CWI.CustomersDB = New CWI.CustomersDB()
Dim rdr As SqlDataReader

rdr = CustInfo.CustomerLogin(tbxEmail.Text, tbxPassword.Text)

If Not rdr.Read() Then
lblLogonFailed.Text = "No such Email/Password"

Else
Session.Add("CustID", rdr.GetValue(0))
Dim cookCustID As New HttpCookie("CID")
cookCustID.Values.Add("CustID", CStr(rdr.GetValue(0)))
Dim dtNow As New DateTime()
Dim tsMinutes As New TimeSpan(0, 2, 0, 0)
dtNow = Now.Add(tsMinutes)
cookCustID.Expires = dtNow
Response.Cookies.Add(cookCustID)
End If
COOKIE & SESSION Retrival logic (standard read logic at end omitted)

Dim CustID As Integer
Dim cookCustID As HttpCookie
Dim context As HttpContext = HttpContext.Current

cookCustID = context.Request.Cookies("CID")

If cookCustID Is Nothing Then
If IsDBNull(context.Session.Item("CustID")) Then
CustID = 0
Else
CustID = CInt(context.Session.Item("CustID"))
End If
Else
CustID = cookCustID.Values.Item("CustID")
End If


Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Xizor | last post by:
Let's say I run a server. I have two people using the server. Bill and Joe. Bill is at address.com/bill and Joe is at address.com/joe. Let's say Joe and Bill are both using PHP with sessions on...
2
by: Jason Telisch | last post by:
I read and reread the PHP manual about sessions, and I have a quick question. What causes the session id to change? I tried destroying the session and unsetting the session vars, but it maintains...
3
by: M Wells | last post by:
Hi All, Just wondering how you go about changing the value of a session cookie via javascript? I have a PHP page that sets a session cookie when it first loads. I'd like to be able to change...
2
by: Steve Remer | last post by:
OK, I think I understand session state pretty well. I've done additional research on Google over the last few days to fill in any holes. To begin with, I'm using StateServer and not InProc for...
14
by: Michael Carr | last post by:
I have an intermittent problem that occurs on a very small number of browsers that access my website. The strange thing is this: my site works perfectly for 99.9% of browsers. The behavior...
4
by: Chris | last post by:
When a request comes into a page on my ASP.net site and a session is not found, I want to detect whether the request is an initial request or if the user did have a session going that has now been...
5
by: jb | last post by:
*Please* help --- I'm tearing my hair out. I want to use sessionstate in a webservice, accessed from a client, written in script (JScript, InfoPath). I have written my webservice (C# .NET). I...
0
by: Brano | last post by:
Hi all, I have a asp.net website that has been live for about 2 weeks now and there were no problems with it. I have got a new server that is Win 2003 IIS 6.0 I have moved my application onto...
8
by: Eddie | last post by:
I am having difficulty in setting variables in a session, and then accessing those variables throughout the web pages that they click on. After having them set a user name and password,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.