Cookie Class

The Cookie class lets you access cookies for your Salesforce site using Apex.

System

Use the setCookies method of the PageReference Class to attach cookies to a page.

  • Cookie names and values set in Apex are URL encoded, that is, characters such as @ are replaced with a percent sign and their hexadecimal representation.
  • The setCookies method adds the prefix “apex__” to the cookie names.
  • Setting a cookie’s value to null sends a cookie with an empty string value instead of setting an expired attribute.
  • After you create a cookie, the properties of the cookie can’t be changed.
  • Be careful when storing sensitive information in cookies. Pages are cached regardless of a cookie value. If you use a cookie value to generate dynamic content, you should disable page caching. For more information, see Configure Site Caching in Salesforce Help.

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.

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.

The following are constructors for Cookie.

Creates a new instance of the Cookie class using the specified name, value, path, age, and the secure setting.

public Cookie(String name, String value, String path, Integer maxAge, Boolean isSecure)

  • name:

    Type: String

    The cookie name. It can’t be null.

  • value:

    Type: String

    The cookie data, such as session ID.

  • path:

    Type: String

    The path from where you can retrieve the cookie.

  • maxAge:

    Type: Integer

    A number representing how long a cookie is valid for in seconds. If set to less than zero, a session cookie is issued. If set to zero, the cookie is deleted.

  • isSecure:

    Type: Boolean

    A value indicating whether the cookie can only be accessed through HTTPS (true) or not (false).

Creates a new instance of the Cookie class using the specified name, value, path, and age, and settings for security and cross-domain behavior.

Google Chrome 80 introduces a new default cookie attribute setting of SameSite, which is set to Lax. Previously, the SameSite cookie attribute defaulted to the value of None. When SameSite is set to None, cookies must be tagged with the isSecure attribute indicating that they require an encrypted HTTPS connection.

public Cookie(String name, String value, String path, Integer maxAge, Boolean isSecure, String SameSite)

  • name:

    Type: String

    The cookie name. It can’t be null.

  • value:

    Type: String

    The cookie data, such as session ID.

  • path:

    Type: String

    The path from where you can retrieve the cookie.

  • maxAge:

    Type: Integer

    A number representing how long a cookie is valid for in seconds. If set to less than zero, a session cookie is issued. If set to zero, the cookie is deleted.

  • isSecure:

    Type: Boolean

    A value indicating whether the cookie can only be accessed through HTTPS (true) or not (false).

  • SameSite:

    Type: String

    The SameSite attribute on a cookie controls its cross-domain behavior. The valid values are None, Lax, and Strict. After the Chrome 80 release, a cookie with a SameSite value of None must also be marked secure by setting a value of None; Secure.

See Also

Creates a new instance of the Cookie class using the specified name, value, path, age, and settings for security, cross-domain behavior, and JavaScript access.

public Cookie(String name, String value, String path, Integer maxAge, Boolean isSecure, String SameSite, Boolean isHttpOnly)

  • name:

    Type: String

    The cookie name. It can’t be null.

  • value:

    Type: String

    The cookie data, such as session ID.

  • path:

    Type: String

    The path from where you can retrieve the cookie.

  • maxAge:

    Type: Integer

    A number representing how long a cookie is valid for in seconds. If set to less than zero, a session cookie is issued. If set to zero, the cookie is deleted.

  • isSecure:

    Type: Boolean

    A value indicating whether the cookie can only be accessed through HTTPS (true) or not (false).

  • SameSite:

    Type: String

    The SameSite attribute on a cookie controls its cross-domain behavior. The valid values are None, Lax, and Strict. After the Chrome 80 release, a cookie with a SameSite value of None must also be marked secure by setting a value of None; Secure.

  • isHttpOnly:

    Type: Boolean

    A value indicating whether the HttpOnly attribute for the cookie is set (true) or not (false). If true, client-side JavaScript can’t access the cookie.

See Also

The following are methods for Cookie. All are instance methods.

  • getDomain()

    Returns the name of the server making the request.

  • getMaxAge()

    Returns a number representing how long the cookie is valid for, in seconds. If set to < 0, a session cookie is issued. If set to 0, the cookie is deleted.

  • getName()

    Returns the name of the cookie. Can’t be null.

  • getPath()

    Returns the path from which you can retrieve the cookie. If null or blank, the location is set to root, or “/”.

  • getSameSite()

    Returns the value for the SameSite attribute of the cookie.

  • getValue()

    Returns the data captured in the cookie, such as Session ID.

  • isSecure()

    Returns true if the cookie can only be accessed through HTTPS, otherwise returns false.

  • isHttpOnly()

    Returns true if client-side JavaScript is forbidden from accessing the cookie; otherwise returns false.

Returns the name of the server making the request.

public String getDomain()

Type: String

Returns a number representing how long the cookie is valid for, in seconds. If set to < 0, a session cookie is issued. If set to 0, the cookie is deleted.

public Integer getMaxAge()

Type: Integer

Returns the name of the cookie. Can’t be null.

public String getName()

Type: String

Returns the path from which you can retrieve the cookie. If null or blank, the location is set to root, or “/”.

public String getPath()

Type: String

Returns the value for the SameSite attribute of the cookie.

public String getSameSite()

Type: String

See Also

Returns the data captured in the cookie, such as Session ID.

public String getValue()

Type: String

Returns true if the cookie can only be accessed through HTTPS, otherwise returns false.

public Boolean isSecure()

Type: Boolean

Returns true if client-side JavaScript is forbidden from accessing the cookie; otherwise returns false.

public Boolean isHttpOnly()

Type: Boolean

See Also