Online documentation - Websydian v6.0

Users Guide | Patterns Reference | WebsydianExpress | Search

 
 

Using Cookies with Websydian

Cookies as Web Application Persistent State Storage

Cookies let you store state information on the client browser's computer for later retrieval. Cookies are one of several techniques for maintaining state within a Web application.

In their simplest form, cookies store data in the form of name=value pairs. The Web application developer can pick any name and value combination. More advanced cookie features include the capability to set an expiration date and to specify what Web pages may see the cookie information.

When a cookie is set on the browser of a user, it may persist for any period of time, even in years. This makes it easy to save information, e.g. user name and user preferences, and to keep this information available every time the user returns to the Web application.

A cookie is associated with the Web site that originally created the cookie, and only Web pages or applications within the originating site can retrieve the values at a later time.

The values are retrieved based on the name part of the name/value pair.cookie.gif (1662 bytes)

Cookies are very powerful and have many advantages, but also one major disadvantage: Users concerned about intrusion into their privacy can (perhaps unrightfully) completely disable cookie usage through the Web browser settings. If cookies are disabled, even well designed cookie-based Web applications will not function at all.

For the remainder of this page it is assumed that the reader knows how to use cookies in Web applications in general.

More Information

More information about cookies can be found at these sites.

 

It can also prove valuable to consult some books, e.g. Richard Wagner et al.: "JavaScript Unleashed". Sams.net Publishing, 1997.

Use of Cookies in Websydian application

The Websydian Session Library and Cookies

For far most purposes, the Session library supplied with Websydian should be used to store the Session State within an application, and cookies should be avoided. In the Session library the persistent session state is stored on a server table within the Advantage Plex application. Al imaginable user information can be stored in server tables with the Websydian Session library.

In some cases, e.g. if a Web application should allow users to reuse previously created sessions or to skip a login page, the session ID that are used to uniquely identify the user session must be stored somehow in between user sessions.

In these cases introduction of cookies are evident. Cookies can be used to store the session ID and/or user information.

Keep in mind, however, that use of cookies should always be designed in such a way, that the Web application could operate even if the Web user has disabled cookies. Cookies should always work as an optional feature.

Set a Cookie

A cookie is set by dynamically adding a JavaScript code piece that sets the value of the cookie property of the document object of the Web browser.

In a Websydian application the session ID should be stored in a cookie. A SIDCookie field with the implementation name "SIDCOOKIE" is placed in the HTML document template:

 

<BODY>

/(SIDCOOKIE)

<H1>Document heading</H1>

At run-time, it is checked whether a cookie already exists. If not, a JavaScript code piece that stores the session id as a cookie is formatted using Format message and placed in the SIDCookie field. The JavaScript code piece are stored in a Advantage Plex message object:

 

<SCRIPT LANGUAGE="JavaScript">

  var oneDay = 24 * 60 * 60 * 1000;

  var expDate = new Date();

  expDate.setTime(expDate.getTime() + oneDay);

  document.cookie="WSYD_SID=&(1:); expires="+expDate.toGMTString();

</SCRIPT>

The AppCookie field are assigned the result of formatting this message with a newly created session ID.

Example part of generated HTML page:

 

<BODY>

<SCRIPT LANGUAGE="JavaScript">

  var oneDay = 24 * 60 * 60 * 1000;

  var expDate = new Date();

  expDate.setTime(expDate.getTime() + oneDay);

  document.cookie="WSYD_SID=1023; expires="+expDate.toGMTString();

</SCRIPT>

<H1>Document heading</H1>

 

The JavaScript code piece will be executed when the Web browser loads the HTML document. The cookie value WSYD_SID will be stored in 24 hours.

Get a Cookie

The cookies available to a specific HTML pages can be retrieved from the HTTP_COOKIE Web server environment variable with the GetHeaderOrEnvironmentVariable function. The WSYD Environment property parameter must be set to the Http cookie value.

All cookie name=value-pairs available to the specific HTML page separated by semicolons are returned in the WSYD Environment value parameter, and the values of interest must be extracted by application code.

Example of returned cookie name=value pairs for a Web application:

 

AppID=23; userID=AF433E001; WSYD_SID=1023;