function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Scott.MScott.M 

Does Salesforce Strip Cookies from the Header for PageReferences?

Hi,

 

I'm trying to use cookies with a visualforce page. I set the cookie with javascript but when I use System.currentPageReference().getHeaders() the returned map doesn't have a 'Cookie' key. Is this because salesforce strips them before they get to us and if so is there a reason for doing this? 

 

Thanks!

Scott 

richardvrichardv

Also, I've noticed the following line of code:

 

ApexPages.currentPage().getHeaders().put('Set-Cookie','xyz=123'); 

 

Will give you the following error:

 

=====================================
Cookies cannot be accessed or set in Apex Pages in this release

 

An unexpected error has occurred. Your development organization has been notified. 

=====================================

 

Is there a workaround for this?  If not, attention to Salesforce Prod Mgmt: developers need to be able to access and set Cookies.  

TehNrdTehNrd
Did either of you ever get a response on this?
dchasmandchasman
Visualforce has always blocked access to cookies server side - various security reasons drove this decision - we do have supporting cookie access on the roadmap once these security concerns can be alleviated.
TehNrdTehNrd

Thanks for the update Doug.

 

I will assume the best way to do this now is upon load of the page invoke some javascript that can read/set the cookies and then perform logic as necessary, either in the script or with an actionFunction.

ericszulcericszulc

Hello.

 

Maybe someone can enlighten me. I'd like to use javascript functions to read/set cookies as well, but I'm running into an issue. I have a form, and after the user submits the form, in the controller class a variable is updated. What I can't seem to figure out (seems trivial) is how do I pass that updated variable from the controller back to setCookie javascript function in my visualforce page (i.e. how do I call setCookie(var) with the updated variable).

 

Thanks in advance.

TehNrdTehNrd

Code below should show you the basics of how this could work.

 

 

public String myVariable {get; set;} publc void someMehtod(){ myVariable = 'test'; } <apex:commandButton value="submit" action="{!someMehthod}" rerender="script"/> On the page: <apex:outputPanel id="script"> <script type="javascript"> var myVar = '{!myVariable}'; alert(myVar); </script> </apex:outputPanel

 -Jason

 

 

 

 

 

ericszulcericszulc

Awesome. Thank you.

 

So simple... My problem was that I kept trying to call the javascript function from within the apex markup.