Session variables can be useful when writing ASP scripts; however, to use them, the client’s browser must accept cookies. When ASP starts a session, it sends a cookie to the browser with a SessionID, which ASP can later use to identify the session and thus maintain state. So if you want to use session variables to maintain state of some sort, you need to find out if cookies are being accepted.
Whenever you move between pages, there is a very simple and reliable way to do this. Request the SessionID in the first page, pass it to a second page, and compare them. If they are the same, cookies are "on;" and if not, the browser is not accepting cookies. Simple enough?
One nice way to implement this is to write the SessionID to a hidden field in a form and submit the form. Then, just retrieve the SessionID from the form data and compare it to the SessionID for the new page. A common use for this check is if you have to save the form field values for some reason (to repopulate the form after an error, for example). Of course, you must know if session variables are available.
Example
sessions1.asp contains the following form. We write the SessionID to a field called "theSessionID" and submit the form to an ASP called sessions2.asp:
In sessions2.asp, we retrieve the SessionID that was passed, grab the current SessionID, and compare the values:
<%
dim theSessionID
theSessionID = Request.Form("theSessionID")
'Now, get the current SessionID and compare it to the first one
If theSessionID = Session.SessionID Then
Response.Write "Cookie are ON"
' use session variables
Else
Response.Write "Cookies are OFF"
' don't use session variables
End If
%>
Another handy use of this technique is when you need to know cookie state immediately upon entry to a site.
Use the default page to get the SessionID, and immediately redirect to a "home page":
default.asp
home.asp
Now, just check for cookie state, and act accordingly:
<%
Dim theSessionID
theSessionID = Request.Querystring("theSessionID")
If theSessionID <> "" Then
If theSessionID = Session.SessionID Then
Response.Write "Cookies are ON"
Else
Response.Write "Cookies are OFF"
End If
Else ‘ the user didn’t come from default.asp, send them back
Response.Redirect "default.asp
End If
%>
You can also force the client to go through the default page by checking for theSessionID. If the value doesn’t exist, they didn’t come from default.asp.
Of course, you also can hide the SessionID in a form in this case. Just use JavaScript to submit the form to home.asp by using "onLoad(form_name_here)" in the BODY tag of default.asp. This is a more stealthy method (nothing shows up in the URL), but I wanted to demonstrate another potentially useful technique.
So, now you have a simple, reliable way to see what your clients’ browsers are up to with regard to cookies. Good luck with your scripting!
Download
You can download the complete source for the sample contained in this article:
ASP Session Manager works by inserting 2 lines of code at the top of your ASP script and have the session serialize itself to any DBMS. There are three licenses available, ranging from a single binary to an enterprise ISP license with source code.
Web farm getting out of control? You need scalability! SA-SessionPro is a simple, yet
very powerful means of making your ASP application persistent--share and save
information on a per-user, per-application, per-server, or per network basis. SA-Session is
like the built in Session object that comes with the IIS Server, except it allows developers
more flexibility. It has the ability to store information without expiration and the ability to
preserve session state across multiple machines in a web farm.
SessionFarm is an Active Server Pages component that allows you to manage session state on multiple servers in a web-farm. SessionFarm utilizes the built-in IIS Session object which means you don't have to learn new methods
and objects. The IIS Session object is stored on either a file-share or a SQL database using optimized stored procedures.
SessionFarm requires no rewriting of your existing code and works by supplementing the existing IIS Session.
For code samples, see http://www.groat.com/sessionfarm/implement.asp.
Responding to the legitimate concerns of Internet consumers, a diverse group of web developers has voluntarily formed an ad-hoc team and prepared the following article. The accompanying technical paper includes explanations of the underlying technologies, the role of cookies, discussions of the positive and negative ramifications of this technology, and selected references on this and related topics. [Read This Article][Top]
A rewrite of part one of a four-part series on Active Server objects. A simple example of creating a Active Server Component in Visual Studio 5.0 using the Active Template Library 2.0. The example component retrieves the user's cookie, if not available issues a new 128-bit cookie. Included in the issue is the source code and step by step instructions. This issue has been rewritten to illustrate the use of Visual Studio 5.0 and ATL 2.0 in writing Active Server Components. [Read This Article][Top]
Mailing List
Want to receive email when the next article is published? Just Click Here to sign up.