WordType Designs
Driven To Distractions©
The Sound of One Hand Clapping©


A rchive Date
[ 10-06-2000 ]
Category
[ Information Technologies ]
sub-Categoy
[ Networking ]

      [Retrieving cookies with VB Script

      To retrieve cookies into VB Script, use the Document.Cookies object. All the cookies valid for this document are in a semicolon-delimited string, like with JavaScript.

      To parse the string, use a function like so (courtesy of microsoft.com web site):


      <SCRIPT language=VBScript>
      Function GetCookie(strVariableName)
      intNameLength = Len(strVariableName)
      intLocation = Instr(Document.Cookie, strVariableName)
      If intLocation = 0 Then
      GetCookie = ""
      Else
      strTemp = Right(Document.Cookie, Len(Document.Cookie) - intLocation + 1)
      If Mid(strTemp, intNameLength + 2, 1) <> "=" Then
      GetCookie = ""
      Else
      intNextSemicolon = Instr(strTemp, ";")
      If intNextSemicolon = 0 Then
      intNextSemicolon = Len(strTemp) + 1
      End If
      If intNextSemicolon = (intNameLength + 2) Then
      GetCookie = ""
      Else
      intValueLength = intNextSemicolon - intNameLength - 2
      GetCookie = Mid(strTemp, intNameLength + 2, intValueLength)
      End If
      End If
      End if
      End Function
      </SCRIPT>
      Calling this Function like so:
      myVar = GetCookie("foo")

      Yields the value bar


      5.7 Limits on Cookie Retrieval
      The main limit on retrieving a cookie is that you can only retrieve cookies that are valid for the document your script resides in. That is, a script on
      www.myserver.com cannot read cookies from www.yourserver.com. This is mainly governed on the browser side, as browsers know the URL that they are accessing, and only transmit cookies for that server across the connection.

      6. ADVANCED TOPICS

      6.1 Introduction

      This section covers topics beyond the normal creation and retrieval of cookies.

      6.2 Clearing a Cookie Value
      When programming a Web site, there are many reasons that you may need to erase a cookie you have created. Often it is because the cookie is no longer needed, or the scheme of your cookie has been altered, and requires resetting.

      The two main steps to clearing a cookie you have created are:


      1. Set the cookie's value to null.
      2. Set the cookie's expiration date to some time in the past.
      For example, if I wish to clear the "foo" cookie forever, I should set it with the HTTP header:

      Content-type: text/html
      Set-Cookie: foo=; path=/; expires Thu, 01-Jan-1970 00:00:00 GMT


      The reason you must do both is that simply setting the expiration to a past time will not change it's value until the browser is closed. That is, all cookie names, values, expirations, etc are resolved once the browser program has been closed. Setting the cookie to null allows you to properly test for the cookie until that resolution.

      6.3 Detecting if cookies are accepted
      To properly detect if a cookie is being accepted via the server, the cookie needs to be set on one HTTP request and read back in another. This cannot be accomplished within 1 request. When using PERL or ASP, try to funnel your visitors through a common page where you can set a test cookie. Then, when the time comes to detect, check for that cookie.

      If you use client-side languages to set a cookie, you can set and read on the same page. Cookies set by JavaScript or VBScript reside in the browser's memory already, so you will know if they have been accepted right away. Check by setting a test value, and then try to read that value back out of the cookie. If the value still exists, the cookie was accepted.

      6.4 The ASP Session Object
      Microsoft's Active Server Pages have a built-in object for the purpose of using session-related variables. The approach is to tie all session values to a single cookie, rather than assign a cookie to each individual value. This can be a big advantage for programmers because keeping track of cookies can become tiresome.

      Internet Information Server (IIS) 3.0 and 4.0 both set a single cookie for every visitor, called ASPSESSIONID. The value set within this cookie points back to a server database which holds the session values set by the programmer. This is all built-in to IIS and doesn't require any additional setup to use. Simply use the Session object as described in the ASP documentation.


      6.5 The LiveWire Client Object
      Netscape's LiveWire also has a built-in object for using session-related variables. Unlike Microsoft's IIS, LiveWire allows you to select how you wish to involve cookies with this object.

      If you use LiveWire's "client-cookie" state maintenance for a site, then you should refer to the right sections in parts 4 and 5, above. This will give a clearer picture on how to use this properly.

      If you use LiveWire's "server-cookie" state maintenance for a site, then the behavior is very similar to that of IIS. A single cookie is sent to the visitor that points to a database on the server where all the session variables are maintained.


      About the Author
      David Whalen is a Software Engineer. David writes Web-based applications in ASP, PERL, Java, and JavaScript. He has worked in the Web industry for 4 years. David is a Microsoft Certified Professional. In addition, he was an inaugural Netscape DevEdge Champion, where he provided third-party support to developers in JavaScript and Netscape's LiveWire.

      David is 29 years old. He holds a Masters Degree in Astrophysics from the University of Wyoming. His personal interests include traveling, photography, gaming, animation, and sporting. When not programming, he can be found either playing Quake 2 or out on the golf course.


      Copyright ©1999 David Whalen. This document is provided "as is" without any guarantees or warranty. Although the author has attempted to find and correct any errors or mistakes he, and everyone who contributed to it, are not responsible for any damage or losses of any kind caused by the use or misuse of information in this FAQ. The author is under no obligation to provide service, corrections, or upgrades to this FAQ.

      The following is legal information and refers to all the information in this document. This information pertains to all use of the FAQ worldwide. All specific names included in the package are registered trademarks and are hereby acknowledged. Any other trademarks not mentioned in the FAQ are still hypothetically acknowledged.

    • No portion of the document may be separated and distributed separately without the written permission of the author, David Whalen.
    • The document can not be included in any publication, such as, but not limited to: magazines, books, newspapers or newsletters, without the written permission of the author.
    • The document can not be included in any software compilation using media such as, but not limited to: CD-ROM, tape backup, optical disks, hard disks or memory cards, without the written permission of the author.
    • The document can not be recompiled, modified or not, and distributed without the written permission of the author.
    • Visitors are encouraged to send submissions and error fixes to the author, but the author is in no way obliged to utilize these enhancements or fixes.
    • In the event of ambiguity or omission within this notice, all rights and ownership are retained by the author.

    • Last Modified: 05/10/1999
          ]


Some pages may require Adobe Acrobat Reader



Copyright and Fair Use Information: The contents of this web site is protected by international copyright laws and may not be reproduced in any form or manner whatsoever, if for the purpose of resale or solicitation of a donation. The essays included here, may be reproduced only if: 1)They are not altered in any way; 2) reproductions must be accompanied by this copyright page ; and 3) it is given freely and without charge.
Fair use: The fair use of copyrighted work, including such use by reproduction in copies or phonorecords or by any other means specified in above sections, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright. In determining whether the use made of a work in any particular case is fair use the factors to be considered include : (1) the purpose and character of the use, including whether the use is of a commercial nature or is for nonprofit educational purposes; (2) the nature of the copyrighted work; (3) the amount and substantiality of the portion used in relation to the copyrighted work as a whole, and; (4) the effect of the use upon the potential market value of the copyrighted work.

Home | About Narrative? |Contact
Copyright © 2025. All Rights Reserved
HAG122125 (1998 -2026)