
<erl>
-export([pserv/0]).

ensure_pcookie_server() ->
    case whereis(pcookie_server) of
        undefined ->
            proc_lib:start(?MODULE, pserv,[]);
        _ ->
            ok
    end.

pserv() ->
    catch begin
              register(pcookie_server, self()),
              T = ets:new(pcookies, [set, public,
                                     named_table]),
              ets:insert(T, {counter, 0})
          end,
    proc_lib:init_ack(ok),
    pserv_loop().

pserv_loop() ->
    receive
        X ->
            pserv_loop()
    end.

setcookie(A) ->
    Num = ets:update_counter(pcookies, counter, 1),
    Data = {(A#arg.headers)#headers.user_agent,
            calendar:local_time(),
            inet:peername(A#arg.clisock)},
    ets:insert(pcookies, {{cookie, Num}, Data}),
    yaws_api:set_cookie("pfoobar",integer_to_list(Num),
                        [{path,"/"}]).



out(A) ->
    ensure_pcookie_server(),
    H = A#arg.headers,
    C = H#headers.cookie,
    case C of
        [Cookie|_] ->
            case string:tokens(Cookie, [$=]) of
                ["pfoobar", NumStr] ->
                    Num = list_to_integer(NumStr),
                    %% cookie allready set
                    case ets:lookup(pcookies, {c, Num}) of
                        [{cookie, Data}] ->
                            ok;  %% cookie already set
                        [] ->
                            setcookie(A)
                    end;
                _ ->
                    setcookie(A)
            end;
        _ ->
            setcookie(A)
    end.
</erl>


<erl>


out(A) -> {ssi, "TAB.inc", [],[]}.
</erl>

<div id="entry">





<h2> Set persistent cookie </h2>

<p>This is the page that set a persistent cookie in
the browser.
<a href="readpcookie.yaws">readpcookie.yaws</a> will
read the cookie and report persistent information.



</div>


<erl>
out(A) -> {ssi, "END2",[],[]}.
</erl>

