Newer Version Available
Cookie Class
Namespace
System
Usage
Use the setCookies method of the PageReference Class to attach cookies to a page.
Consider the following limitations when using the Cookie class:
- The Cookie class can only be accessed using Apex that is saved using the Salesforce API version 19 and above.
- The maximum number of cookies that can be set per Salesforce Sites domain depends on your browser. Newer browsers have higher limits than older ones.
- Cookies must be less than 4K, including name and attributes.
- The maximum header size of a Visualforce page, including cookies, is 8,192 bytes.
For more information on sites, see “Salesforce Sites” in the Salesforce online help.
Example
The following example creates a class, CookieController, which is used with a Visualforce page (see markup below) to update a counter each time a user displays a page. The number of times a user goes to the page is stored in a cookie.
The following is the Visualforce page that
uses the CookieController Apex controller
above. The action {!count} calls the getCount method in the controller
above.