ASPFAQs.com
Your source for ASP-related frequently asked questions and answers!


4GuysFromRolla.com : ASP FAQS : Session Object

Question: For session variables to work, must the Web visitor have cookies enabled?

Answer: Kind of. Session variables are bits of information that can be stored on a user-by-user basis. These bits of information are stored on the Web server. To tie these bits of memory with a particular user, for session variables to persist across Web pages, the user must have cookies enabled. When a user first visits a page on a site that uses Session variables, two things happen:

-- A block of memory is allocated for that particular user's session variables on the Web server. This block of memory is identified by a unique SessionID
-- A cookie is written to the user's computer, storing the value of the SessionID

When the user visits another page on the site that uses session variables, the users SessionID cookie is referenced to determine if the user already has a session variable memory block setup. If he does, the values that are stored there are referenced. Through this mechanism, one can create seemingly "global" variables that persist from one page to another.

But what if the user doesn't have cookies enabled? Session variables can still be used, but there will be no way for the user to store the SessionID as he jumps from page to page. Hence, the session variables will not be saved for the user who has cookies disabled. However, session variables that are created and referenced on the same page will work fine. For example, the following script will output "Hello, World" regardless of whether or not the user has cookies enabled:

<%
  Session("Message") = "Hello, World!"
  Response.Write Session("Message")
%>

Of course, if, on another page, the value of Session("Message") is referenced, only those users who have cookies enabled will have a value of "Hello, World!" there.

For more information on Session variables (and info on maintaining state, in general, on the Web), be sure to read the sample chapter from my book,
Sams Teach Yourself Active Server Pages 3.0 in 21 Days!


FAQ posted by Scott Mitchell at 9/26/2000 11:13:06 PM to the Session Object category. This FAQ has been viewed 133,635 times.


Copyright 2014 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers