How to create and retrieve cookie with formula?

Hiya folks,

Does anyone know how to create a cookie (eg. username=“joe”) and later retrieve or reference that cookie using only formula?

The help is not quite helpful on this. The usage of @SetHTTPHeader is not clearly explained, as it seems to use a field called “Set-Cookie”, but it doesn’t explain how to create the “Set-Cookie” field etc etc.

If anyone could help it would be much appreciated.

Subject: How to create and retrieve cookie with formula?

The “Set-Cookie” is not the name of the cookie - it is only a command for the @SetHTTPHeader function. This command tells the function to create or modify an existing cookie. The name of the cookie is determined by the value entered before the equal sign (“=”). So in this case:@SetHTTPHeader(“Set-Cookie”; “SHOP-CART=4444”)

the command will first try to modify an existing cookie with the name “SHOP-CART”. If this cookie isn’t found - then it is created.

On the other hand - I have no idea of how to retreive the cookie value with only @Formula - it seems that You are forced to used JavaScript for this…

Subject: How to retrieve cookie with formula?

No, you can do that with @gethttpheader.

You’ll need to parse the information you get from the @GetHttpHeader(“Cookie”). You’ll get a list of all cookies …

Below is an example from a live application.

cheers,

Tom


_cookie := @GetHTTPHeader(“Cookie”);

REM {Cookie should now have all cookies};

@If( @Trim(_cookie)=“”;

@Do(

	_cookie := @NameLookup( [NoUpdate] ; @UserName ; "preferredLanguage" );

	@If(_cookie="";



		@SetHTTPHeader("Set-Cookie"; "BGC_preferredLanguage=" + "nl-BE" + "; expires=" + @Text(@Yesterday ; "S2") );





 		@SetHTTPHeader("Set-Cookie"; "BGC_preferredLanguage=" + _cookie + "; expires=" + @Text(@Adjust(@Now;0;0;7;0;0;0) ; "S2"))

	)

);

@Do(

@For( x := 1 ; x <= @Elements(_cookie) ; x:=x+1 ;

	@If( @Contains( _cookie[x] ; "BGC_preferredLanguage" ) ;

		_oldCookie := @Middle( _cookie[x] ; "BGC_preferredLanguage=" ; ";" )

		;

	"")

	

);

_cookie := _oldCookie

)

);

@If(_cookie=“”;“nl-BE”;_cookie)

Subject: RE: How to retrieve cookie with formula?

Thanks very much for your help, that certainly clears things up, and I appreciate the code for parsing the cookie.

One problem though:

When I run @SetHTTPHeader (from a ‘sign-in’ button on a form) it doesn’t create a cookie.

Am I using this command in the right context?

I mean, if I put a button on the form, and the button’s action is

@SetHTTPHeader(“Set-Cookie” ; “user=luke”)

will that create a cookie when the button is clicked?

Hope to hear from you soon,

Luke

Subject: RE: How to retrieve cookie with formula?

No, it only works on forms.

You can use a simple javascript to do that:

function setLanguage(newLanguage){

document.cookie = BGC_preferredLanguage="+newLanguage + “;”; window.location.reload();

}

cheers,

Tom

Subject: RE: How to retrieve cookie with formula?

Thanks again for your help.

Subject: RE: How to retrieve cookie with formula?

This may have been something I was doing wrong, but I couldn’t get the cookies to save until I changed the expires= to the format expires=Fri, 31-Dec-2004 00:00:00 GMT

Using @Text with @Now to set the expire date of the cookie didn’t work (for me), just thought I add this to the thread in case someone else runs into the same problem.