Cookie functions

Unofficial Content

Their purpose is to provide functions that allow reading and saving cookies from web objects generated by GeneXus.

What are cookies?

Cookies are small units of data which are saved from a web site on the client machines. CGI programs or any application running on a server can read or save cookies on the client.

Cookies are most frequently used to identify users. When a user registers with a web site (Portal or E-Store), the site saves a cookie in the client machine with the client identification. In this way, the next time the client visits this website, it tries to read the cookie; if it exists, it uses the cookie’s value to identify the user and retrieve his preferences from a database.

There are other ways to use cookies, such as content rotation (especially ads), to maintain an application status, etc. They can even be used as a method to store the shopping cart so that its data is saved in the client machine and is maintained between connections.

As mentioned above, in general a cookie is used to identify the user (in some cases there’s one for each session, while in others a cookie is created for the user), even though all preference values may be included in it. What’s best is to create a key that travels and use it to read the user's data. In this way, the user’s data doesn’t travel to the client, nor is it saved in the URL (for example, credit cards, name and address); it simply remains in the server.

It should be taken into account that there is a limit as to the number of cookies that the client can accept (more info at:  http://support.microsoft.com/kb/306070).

The user may prefer not to store the cookie permanently (for example, if he’s using a public machine such as in an internet cafe) or even may disable cookies. For this reason, cookies mustn’t be the only way to identify users; an alternative method must be available in case the browser doesn't support them or they have been disabled.

Another feature is that the place where cookies are stored depends on the browser (at least in Windows). Therefore, if a user has more than one browser, each one of them will have its own set of cookies.
 
The life cycle of a cookie is as follows:

  1. The user connects to a server that for some reason requires saving a cookie.
  2. The response (HTML Headers) indicates the name and value of the cookie to be saved, as well as other values (the most relevant one is its expiry date).
  3. The browser receives the response and if the expiry date value is in the future, it saves it; otherwise, it searches for one with that name and deletes it.
  4. Every time the user connects to a URL of this domain, the browser will send to the server the cookies saved from the domain that haven’t expired.
  5. Once the expiry date is due, the cookies are deleted.

To obtain more information about cookies and how they are used:  http://www.cookiecentral.com.

The following functions are available and can be used in any GeneXus object. The result only makes sense if said object was called directly or indirectly by a web object running on a web environment.

GetCookie function It allows reading a cookie and returns a string with its value. If it doesn’t find the cookie (or it can’t be read because it is disabled), an empty string is returned.
SetCookie function It allows saving a cookie. It returns 0 (zero) if the result is correct, or another value if an error occurred.

 

See also

Cookie data type