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 Force.com domain depends on your browser. Newer browsers have higher limits than older ones.
- Cookies must be less than 4K, including name and attributes.
For more information on sites, see “Force.com Sites Overview” 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.
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// A Visualforce controller class that creates a cookie
18// used to keep track of how often a user displays a page
19public class CookieController {
20
21 public CookieController() {
22 Cookie counter = ApexPages.currentPage().getCookies().get('counter');
23
24 // If this is the first time the user is accessing the page,
25 // create a new cookie with name 'counter', an initial value of '1',
26 // path 'null', maxAge '-1', and isSecure 'false'.
27 if (counter == null) {
28 counter = new Cookie('counter','1',null,-1,false);
29 } else {
30 // If this isn't the first time the user is accessing the page
31 // create a new cookie, incrementing the value of the original count by 1
32 Integer count = Integer.valueOf(counter.getValue());
33 counter = new Cookie('counter', String.valueOf(count+1),null,-1,false);
34 }
35
36 // Set the new cookie for the page
37 ApexPages.currentPage().setCookies(new Cookie[]{counter});
38 }
39
40 // This method is used by the Visualforce action {!count} to display the current
41 // value of the number of times a user had displayed a page.
42 // This value is stored in the cookie.
43 public String getCount() {
44 Cookie counter = ApexPages.currentPage().getCookies().get('counter');
45 if(counter == null) {
46 return '0';
47 }
48 return counter.getValue();
49 }
50}1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// Test class for the Visualforce controller
18@isTest
19private class CookieControllerTest {
20 // Test method for verifying the positive test case
21 static testMethod void testCounter() {
22 //first page view
23 CookieController controller = new CookieController();
24 System.assert(controller.getCount() == '1');
25
26 //second page view
27 controller = new CookieController();
28 System.assert(controller.getCount() == '2');
29 }
30}The following is the Visualforce page that uses the CookieController Apex controller above. The action {!count} calls the getCount method in the controller above.
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="CookieController">
18You have seen this page {!count} times
19</apex:page>