Find your content:

Search form

You are here

How is a Cookie constructed?

 
Share

The documentation for the Cookie class is here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_sites_cookie.htm

But lacks documentation for the constructor. From code examples it looks like the constructor takes 5 parameters:

  1. String name // cookie name
  2. mixed value // cookie content
  3. ?? // normally 'null'
  4. int time // -1 for expiry with session, otherwise number of seconds to live
  5. boolean ?? // normally 'false'

Can someone confirm this and fill in the blanks for us?

Also, how do you know this?


Attribution to: JannieT

Possible Suggestion/Solution #1

I had the same issue, found this thread then went back and told SFDC that they needed to improve their documentation. Then I read the code comments in the sample, d'OH! (emphasis added)

// create a new cookie with name 'counter', an initial value of '1', 
// path 'null', maxAge '-1', and isSecure 'false'. 
    if (counter == null) {
        counter = new Cookie('counter','1',null,-1,false);

Attribution to: a_sdfc_developer

Possible Suggestion/Solution #2

Just posted this onto your other question:

I have used cookies in the past, and struggled in the same way that you did. My understanding of the constructor is as follows:

Cookie cook=new Cookie('Name', 'Keir Bowden', null, 1440, false);

Where:

  • 'Name' = the name of the cookie
  • 'Keir Bowden' = the value of the cookie
  • null = the cookie path (where null results in the default location '/')
  • 1440 = the max age
  • false = is the cookie only accessible through https

Pretty sure I found this through trial and error and inspecting the HTTP messages - its still my opinion though!

I also recall that the max age didn't seem to have any effect, and I could only generate session cookies, but in the end I couldn't do what I wanted to anyway so I didn't investigate further.


Attribution to: Bob Buzzard

Possible Suggestion/Solution #3

langCookie = new Cookie('uLang', userLanguage, null, -1, false);
    ApexPages.currentPage().setCookies(new Cookie[]{langCookie});
    userLanguage = langCookie.getValue();

Just agreeing with Bob and this is the code that i used for one of the website development on force.com and works perfectly for me .


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #4

May be this helps ..

Below is code sample to read and set apex cookies.

//Setting Cookie
public void setCookie() {
    Cookie userCookie = new Cookie('CookieName', fieldValueToBeStoredAsCookie, null, 315569260, false); //Here 315569260 represents cookie expiry date = 10 years. You can set this to what ever expiry date you want. Read apex docs for more details.
    ApexPages.currentPage().setCookies(new Cookie[] {
        userCookie
    });
}

//Reading Cookie 
Cookie cookie = ApexPages.currentPage().getCookies().get('CookieName');
if (cookie != null) {
    String fieldValueToBeStoredAsCookie = cookie.getValue();        
}

Attribution to: Chirag Mehta
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4894

My Block Status

My Block Content