1

Is there function like php setcookie . If not then how?

EDITED:

yaws_api:setcookie("lang","lang_value", "/"),
yaws_api:find_cookie_val("lang", (A#arg.headers)#headers.cookie),

find_cookie_val returns empty string because setcookie didnt set coockie.

Yola
  • 18,496
  • 11
  • 65
  • 106
  • Did you try to type your question title in your favorite search engine's search box? What results did that give? What specific problem do you have? – Mat Feb 28 '12 at 14:30
  • @Mat, yes i did, but cant get it to work, wrote my code into question post. – Yola Feb 28 '12 at 14:53
  • Do you really have code like that? The cookie most likely can't be queried in the same request you're setting it. It should be findable in subsequent requests though. – Mat Feb 28 '12 at 14:58
  • @Mat,i just refresh page two times. – Yola Feb 28 '12 at 15:03
  • @Mat is correct, the code above won't work because the A arg isn't modified by yaws_api:setcookie. – Steve Vinoski Feb 29 '12 at 04:00
  • @Steve Vinoski, even if i refresh page? I thought that in first time set cookies and after refresh i can read it. – Yola Feb 29 '12 at 09:18
  • @Steve Vinoski, hmm.. i found out it, setcookie only format string that i must return from out(A). – Yola Feb 29 '12 at 10:11

2 Answers2

2

Finally i did it through http header:

out(A) ->
% read 
  yaws_api:find_cookie_val("lang", (A#arg.headers)#headers.cookie),

% send to browser 
  [{header,["Set-Cookie:","lang=ruler;","expires="++to_cookie_expire(60*60*24*30)++";"]},
  {html, ...}]
end.

to_cookie_expire(SecondsToLive) -> 
  Seconds = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
  DateTime = calendar:gregorian_seconds_to_datetime(Seconds +
  SecondsToLive), httpd_util:rfc1123_date(DateTime).
Yola
  • 18,496
  • 11
  • 65
  • 106
  • Why are you doing this? use yaws_api:set_cookie(name, value, options) you can test in Chrome debug console -> Network and click the request, then click the "cookie" tab, there you will find request and response cookies. – Aus Aug 04 '16 at 15:28
  • Also it can be that you're using an old yaws version. full of bugs. upgrade to latest. – Aus Aug 04 '16 at 15:29
  • Forget my first comment, actually if you use yaws_api:set_cookie/3 you cannot add more stuff in your response, like json or html. I hope I'm doing something wrong because this is really impractical. – Aus Aug 05 '16 at 15:47
1

This site provides some example code for yaws and setting cookies: http://yaws.hyber.org/cookies.yaws

Baxter
  • 5,633
  • 24
  • 69
  • 105
  • 1
    But @Baxter is right, the code on the cookies.yaws pages explains exactly how to do this. You don't need to set a Set-Cookie header directly, just use yaws_api:set cookie. – Steve Vinoski Feb 29 '12 at 03:59