asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

How to Detect If Cookies Are ON
By Mark Burnham
Rating: 3.3 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction


    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:

    
    <form name="Form1" method="post" action="sessions2.asp">
    UserName:<input name="username"><br>
    Password:<input name="userpassword">
    <input type="hidden" name="theSessionID" value="<%=Session.SessionID%>">
    <br><input type="submit" value="Submit">
    </form>
    
    
    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

    
    <%  
    Response.Redirect "home.asp?theSessionID=" & Session.SessionID
    %>
    
    
    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:

    http://15seconds.com/files/991223.zip

    About the Author

    Mark Burnham is President of Manhattan Heavy Industries, Inc., a New York City based Internet development company.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    ASP Session Manager
    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.
    [Top]
    SA-Session Pro
    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.
    [Top]
    SessionFarm
    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.

    [Top]
    Other Articles
    Jun 23, 1997 - Cookies and The Web: The Goal is Rich Interactivity
    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]
    Apr 22, 1997 - Active Server Components with VS 5.0
    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.

    Support the Active Server Industry