Creating and Retrieving Cookies

Cookies can be created in a many different languages and environments a few examples are:

Active Server Pages - using JavaScript

Active Server Pages have an in-built object - Response.Cookies Object which contains parameters equivalent to each possible parameter in the normal HTTP header:

Response.Cookies("this_ name") = "this_value";
Response.Cookies("this_ name").Expires = ExpireDate.getVarDate();
Response.Cookies("this_ name").Domain = "myserver.com";
Response.Cookies("this_ name").Path = "/";
Response.Cookies("this_ name").Secure = false;

The Request.Cookies object is used to retrieve cookies into ASP. All the cookies valid for this document are pre-parsed into this object for easy access. To assign a value to the variable My_value write:

My_value = Request.Cookies("this-name"); 

My_value  now contains  this-value

JavaScript

JavaScript has a built-in object called document.cookie to create, store and retrieve cookies. Inserting the minimum number of mandatory parameter values into document.cookie will create a cookie. The syntax is:

<SCRIPT LANGUAGE="JavaScript">
document.cookie = "this_ name=this_ value; +
                                 path=/; +
                                 expires=Sat, 01-Jan-2000 00:00:00 GMT";
</SCRIPT>

JavaScript also uses document.cookie to retrieve cookies. document.cookie would have a string:

This_name=this_value; this=that; somename=somevalue;.....

containing every name-value pair valid for this document, separated by semicolons. These would usually be extracted by writing a function to send the values to variables in the routine which would be using them.

PERL

In PERL, cookies are created by writing an actual header to the response for an HTTP request. A simple cookie header could be written:

Content-type: text/html
Set-Cookie: this_name=this_value; path=/; expires=Sat, 01-Jan-2000 00:00:00 GMT

An HTML page would follow this.

Cookies can be read into PERL with the Environment Variable $ENV{'HTTP_COOKIE'} which contains the valid name-value pairs for this document, separated by semicolons. These would generally be retrieved by writing a parsing routine.

VBScript

Creating a cookie in VBScript uses the document.cookie object and is almost identical to that of JavaScript:

<SCRIPT LANGUAGE="VBScript">
document.cookie = "this_name=this_value; &_
                                path=/; &_
                                expires Sat, 01-Jan-2000 00:00:00 GMT"
</SCRIPT>

To retrieve cookies into VB Script, use the document.cookie object. Again the name-value pairs of cookies valid for this document are in a string, as with JavaScript. One would write a function to parse the string – say getCookie which would be used so:

My_value = getCookie("this_name")

which would give the value this_value