Technical References - [cookie]

Cookies are a great way to remember visitors, and to store variables from page to page.

[setcookie name=cookieName&value=cookieValue&expires=expireDate&path=/&domain=yourdomain.com]


Putting a [setcookie] tag into a template causes the remote browser to create or replace a cookie of that name in its local list of cookies. You can use [getcookie] or [listcookies] later to retrieve that value from the remote browser. The expiration date of the cookie must be of the form Wednesday, 09-Nov-1999 23:12:40 GMT. The domain must be the name of the domain being served, otherwise the browser will not provide the cookie information.

Example:
[setcookie name=Member&value=T&expires=Wednesday, 09-Nov-2008 23:12:40 GMT&path=/&domain=yourdomain.com]


or one that expires 7 days from today (the +05:00:00 handles the US East Coast time difference from GMT):

[setcookie name=cart&value2515983408&expires=[format days_to_date %A, %d-%b-%Y][math]{[date]}+7[/math][/format] [math time]{[time]}+{05:00:00}[/math] GMT&path=/&domain=yourdomain.com]


(Configuring the messy code of the expires value is an excellent job for a global [function].)

If you do not specify an expires parameter, then the cookie becomes a "session cookie," expiring automatically when the browser quits. That is, omit "expires=" altogether; don't set it with a null value - that will fail.

The default value for path, '/', will allow any template on the website to access the cookie. You can, however, confine a cookie to a specific directory. For instance, if you specify a path value of '/admin/', then only templates within the '/admin' directory can access the cookie.

Setting the domain parameter limits which subdomain may access the cookie. If you specify 'www.yourdomain.com', then only templates within that subdomain can access the cookie. Specifying just 'yourdomain.com' for the domain, will allow templates from 'www.yourdomain.com', 'secure.yourdomain.com', or 'whatever.yourdomain.com' to all access the cookie.

RETRIEVING COOKIES

[getcookie name=cookieName]
Displays the value of the cookie named "member" that the remote browser has remembered. If no cookie of that name exists, then nothing (a blank value) is returned.

[ listcookies params]Cookie Tags[/listcookies]
ListCookies lists all the cookie names and values sent from the remote browser. ListCookies is very helpful when writing code using cookies, to see if and when cookies are being set.

Example:
The following are all the cookies available to this page:<br>
[listcookies]
[index], [name], [value]<br>
[/listcookies]


The [listcookies] context has optional parameters to modify the list of cookies produced. To list the cookies whose name begins with "text":
[listcookies name=text&exact=F]
[index],[value]<br>
[/listcookies]


Parameter Description
Name(optional) The name of the cookie to list.
ExactT or F (optional) Whether to exactly match the cookie name or return any cookie name that contains the "name" value. (Default value is T.)


Tags available inside a [listcookies] context:
Tag Description
[name]The name of the cookie.
[value]The value associated with the cookie.
[index]A number indicating this cookie's placement in the list.


It is important to understand that when you set a cookie on a page, it is NOT available via [getcookie] further down in the same page. Only when the user accesses the NEXT page does the cookie become available. For this reason, [redirect [thisurl]] is often used immediately after setting a cookie to make it available.



New [setcookie] parameters (from WebDNA 8.5)



Parameter Description
HttpOnly(optional) HttpOnly should be T, just like Secure. It adds a HttpOnly to the cookie, and treats everything else as a F.
MaxAgeMaxAge should be a date just like Expires. It adds a Max-Age= to the cookie
RawRAW lets you add anything you want to the cookie. You can use it conjunction with the other parameters or in place of them. You must still specify a name and value. Separate multiple raw parameters with a semi-colon and a space.

For example:
[setcookie name=cookieName&value=cookieValue&HttpOnly=T&Raw=domain=example.com; secure]
or
[setcookie name=cookieName&value=cookieValue&Raw=domain=example.com; secure; HttpOnly]




Terry Wilson

DOWNLOAD WEBDNA NOW!

Top Articles:

[biotype]

BioType is a behavioral biometrics WebDNA function based on ADGS research and development (from version 8...

WebDNA Libraries

A list of available libraries for WebDNA...

F.A.Q

A compilation of some user's questions...

Download WebDNA Applications

WebDNA applications...

WebDNA Modules

A list of the currently available modules...

Tips and Tricks

A list of user-submitted tips ...

Related Readings:

[findstring]

[FindString Source=...

[DOS]

Executes the DOS batch file commands contained in the context and displays the results...

Database format

...

[replace]

Replaces each found record in a database with the new field values...

[hideif]

[HideIf Comparison]Hide This HTML[/HideIf]...

[spawn]

Creates a new thread to execute WebDNA simultaneously with the current template...