Persistent Cookies
[Previous] [Main] [Next]

 
htmlCookie provides automated access for sending and receiving persistent HTTP cookies to and from web browsers. Cookies are a general mechanism which server applications can use to store and retrieve information from web browsers. Cookies greatly extend the capabilities of html++ applications.  
 
A server, when transmitting a page to a browser, may optionally send a user-defined value, called a cookie, that represents state information which the browser will store. Included in the cookie is a description of the range of URL's for which that state is valid, and a date after which the browser may delete the cookie. The browser will send the cookie back to the server for any future page requests within the range of specified URL's. The htmlCookie class encapsulates all aspects of cookie manipulation, making their use both easy and convenient.  
 
By using the htmlCookie class with the appropriate htmlServer dervied class (htmlCgi or htmlISAPI), your applications will be able to recognize individual browsers and store information about selections, user id's, passwords, and more.  
 
Only one cookie object is allowed per page. Multiple name/value pairs (NetScape has a limit of 20 per domain, not to exceed 4K total) can be specified, though typically only one is all that's needed for storing a database record number or some other unique identifier.  
 
The htmlServer::Cookie() method provides access to the cookie object for a session, which is created automatically during htmlServer construction. To see if a cookie was sent with a page request by the browser, use the inherited CNVList::Exists() method to check for a cookie value by name. The following is an example:  
 
   htmlCgi   server( cin ) ;  
   if ( server.Cookie().Exists( "customerid" ) )  
   {  
      String  value = server.Cookie().Value( "customerid" ) ;  
   }  
 
To replace the cookie object in a browser, use the << operator or the htmlServer::Add() base-class method:  
 
   htmlCgi  server ;  
   if ( ! server.Cookie().Exists( "customerid" ) )  
   {  
      //  Create a new cookie  
      htmlCookie   cookie ;  
      cookie.Value( "customerid""1234" )  
            .Expires( +365 ) ;  
      server << cookie ;  
   }  
 
Most browsers have a limit on the number of cookies that can be stored. NetScape Navigator imposes the following limits:  
  • 300 total cookies
  • 20 cookies per host or domain
  • 4K per cookie
 
To delete a cookie from a browser, set a cookie with the same name using an expire date in the past. The path and name must exactly match the ones stored in the browser in order for the replacement to occur. You may find it convenient to use the Expire() method with an argument of negative one (-1), indicating a date of yesterday.  
 
Note that there is a bug in Netscape Navigator version 1.1 and earlier. Only cookies whose path attribute is set explicitly to "/" will be properly saved between sessions if they have an expires attribute.  
 
For a complete example, refer to the sample application included in the htmlCookie section of the Class Reference.  


©1998 DC Micro Development. All rights reserved.
No portion of this document may be c opied or reproduced without expressed written consent.
html++ is a trademark of DC Micro Development.