2

Iam using custom object to store user information. When user logged in am storing username in cookies. So, user user logged into home page and access their information using cookie value.

Once user click on log out button, am clearing cookie values.

If user logs in,closes webpage, then again open the same webpage, user is still logged in. This is working fine until user shut down/restart the system.

Code snippet:

    Cookie usernameCookie= new Cookie('uname',username,null,-1,false);
    ApexPages.currentPage().setCookies(new Cookie[]{usernameCookie});

    PageReference myHome= Page.HOME;
    myHome.setRedirect(true);
    return myHome;

Once user shut down/restart the system and then open the same webpage, user is logged out(which mean cleared the cookies).

For example, if user logs in,closes webpage, then days or weeks later, user is still logged in when open the same webpage.

How can i maintain the cookie values up to 1 month or until user logs out.

Any help required.

3

You can set the cookie Max Age parameter in seconds.

Its 4th parameter in in your code.

If you want to set it for one day set maxAge to 86400

If you want to set it for 30 days day set maxAge to 86400 *30 for ex.

Cookie usernameCookie= new Cookie('uname',username,null,(86400 *30),false);

Ref: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_sites_cookie.htm

  • thanks for your reply. By using maxAge param cookie valid for in seconds. Is there any way to maintain the cookie values until user logs out. – Murali Mohan M Jun 1 '16 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.