help files
Z505 | PasWiki | FUQ | Search | Main Docs | API Guide



[Overview][Constants][Procedures and functions] Reference for unit 'pwumain' (#powtils_main)

SetCookie

Declaration

Source position: pwumain.pas line 83

function SetCookie(

  const name: String;

  const value: String

):Boolean;

Notes

Sets a cookie variable.

Like web headers, cookies must be set before any output. Use this function prior to functions such as templateout, outln, webwrite, etc.

To unset a cookie, use the UnsetCookie function.

To get (retrieve) the cookie value at a later time, use the GetCookie function.

The function returns TRUE on success, FALSE on error.

Tips

The expiry of the SetCookie function is set into the future so that the cookie will not expire until you make a call to UnsetCookie. If you want to have more control over the expiry, use the SetCookieEx function. In Powtils versions greater than 1.6.1.1 the default future expiry date can be found in pwmain.pas global constant called SETCOOKIE_FUTURE. For example year 2020 is in the future at current date of writing. This constant will be updated as time goes on.

If output_buffering in your config file is disabled, this call is valid only before any output calls (outln, webwrite, templateout, etc.).

Example

begin
  SetCookie('foo', 'bar');
  s := GetCookie('foo');
  webwrite('What is the cookie value: ');
  webwrite(s);
end.

Cookies vs Sessions

Cookies are used to maintain state on the visitors computer. Sessions are used to maintain state on the server. Sessions are meant for shorter term state storage and cookies are meant for longer term storage. However sessions can be used like cookies, and they overlap in functionality.

If you wanted to store state for say 15-90 days it is recommended you use cookies rather than sessions, usually (but rules can be broken..). The users computer should still have the cookie assuming he did not delete it. Cookies are somewhat more secure than sessions for longer periods of time (although again, this is debatable).

See also

GetCookie, SetCookieEx






lufdoc, Powtils, fpc, freepascal, delphi, kylix, c/c++, mysql, cgi web framework docs, Z505