0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VisualforceでCookieを利用する

0
Last updated at Posted at 2020-09-14

Cookieに登録

登録
public void writeCookie(String key, String value) {
    Cookie c = new Cookie(key, value, null, -1, true);
    ApexPages.currentPage().setCookies(new List<Cookie>{c});
}

コンストラクタの引数

  • Cookie(name, value, path, maxAge, isSecure)
    • String name ― Cookieの名前。
    • String value ― Cookieの値。
    • String path ― ブラウザがCookieを送信するURLのディレクトリ。
    • Integer maxAge ― Cookieの有効期限(秒)。負の値を設定するとブラウザが閉じるまで有効。0を設定するとCookieが削除される。
    • Boolean isSecure ― trueを設定するとHTTPSのみCookieにアクセスできる。
  • Cookie(name, value, path, maxAge, isSecure, sameSite)
    • String sameSite ― クロスドメイン時の動作。有効な値はNoneLaxStrict

注意

  • ブラウザに保存されるCookie名はプレフィックスapex__が追加される。

Cookieを取得

取得
public string getCookie(String key) {
    Cookie c = ApexPages.currentPage().getCookies().get(key);
    return c?.getValue();
}

注意

  • ApexのsetCookiesメソッドによって設定されたCookieのみ取得可能。
  • setCookiesメソッドによって設定されたCookieはプレフィックスapex__が追加されるが、getCookiesメソッドはプレフィックスなしの状態で取得される。

Cookieを削除

削除
public void removeCookie(String key) {
    Cookie c = new Cookie(key, '', null, 0, true);
    ApexPages.currentPage().setCookies(new List<Cookie>{c});
}

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?