この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Cookie クラス

Cookie クラスにより、Apex を使用して Salesforce サイトの Cookie にアクセスできます。

名前空間

System

使用方法

PageReference クラスsetCookies メソッドを使用して、ページに Cookie を添付します。
  • Apex の Cookie 名と値セットは URL 符号化されています。つまり、@ などの文字は % 記号および 16 進数表現に置き換えられます。
  • setCookies メソッドは Cookie 名にプレフィックス「apex__」を追加します。
  • Cookie の値を null に設定すると、期限切れの属性の設定ではなく、空の文字列値の Cookie を送信します。
  • Cookie の作成後は、Cookie のプロパティを変更することはできません。
  • 機密情報を Cookie に格納する場合は注意してください。Cookie の値に関係なくページはキャッシュされます。動的なコンテンツを生成するために Cookie の値を使用する場合は、ページキャッシュを無効にする必要があります。詳細は、Salesforce ヘルプの「Salesforce サイトページのキャッシュ」を参照してください。

重要

Cookie クラスを使用する場合は、次の制限に留意してください。
  • Cookie クラスには、Salesforce API バージョン 19 以降を使用して保存されている Apex を使用することでのみアクセスできます。
  • Salesforce サイトドメインごとに設定できる Cookie の最大数はブラウザーにより異なります。新しいブラウザーは古いブラウザーより高い制限が設定されています。
  • Cookie は名前および属性を含め 4K 未満である必要があります。
  • Visualforce ページの最大ヘッダーサイズは、Cookie を含めて 8,192 バイトです。

サイトについての詳細は、Salesforce オンラインヘルプの「Salesforce サイト」を参照してください。

次の例では、CookieController クラスを作成します。このクラスは Visualforce ページ (下記マークアップを参照) を使用して、ユーザーにページが表示されるたびにカウンターが更新されます。ページへのアクセス回数が Cookie に保存されます。

1// A Visualforce controller class that creates a cookie
2// used to keep track of how often a user displays a page
3public class CookieController {
4
5    public CookieController() {
6        Cookie counter = ApexPages.currentPage().getCookies().get('counter');
7    
8        // If this is the first time the user is accessing the page, 
9        // create a new cookie with name 'counter', an initial value of '1', 
10        // path 'null', maxAge '-1', and isSecure 'true'. 
11        if (counter == null) {
12            counter = new Cookie('counter','1',null,-1,true);
13        } else {
14        // If this isn't the first time the user is accessing the page
15        // create a new cookie, incrementing the value of the original count by 1
16            Integer count = Integer.valueOf(counter.getValue());
17            counter = new Cookie('counter', String.valueOf(count+1),null,-1,true);
18        }
19    
20        // Set the new cookie for the page
21        ApexPages.currentPage().setCookies(new Cookie[]{counter});
22    }
23
24    // This method is used by the Visualforce action {!count} to display the current 
25    // value of the number of times a user had displayed a page. 
26    // This value is stored in the cookie.
27    public String getCount() {
28        Cookie counter = ApexPages.currentPage().getCookies().get('counter');
29        if(counter == null) {
30            return '0';
31        }
32        return counter.getValue();
33    }
34}
1// Test class for the Visualforce controller
2@isTest
3private class CookieControllerTest {
4  // Test method for verifying the positive test case
5  static testMethod void testCounter() {
6    //first page view
7    CookieController controller = new CookieController();
8    System.assert(controller.getCount() == '1');
9
10    //second page view       
11    controller = new CookieController();
12    System.assert(controller.getCount() == '2');
13  }
14}
次は、上記の CookieController Apex コントローラーを使用する Visualforce ページです。アクション {!count} では、上記のコントローラーで getCount メソッドをコールします。
1<apex:page controller="CookieController">
2You have seen this page {!count} times
3</apex:page>