#!/home/janweb/www/hugs/bin/runhugs +l
-- Hey, Emacs, this is -*- haskell -*-
-- Cookie example by Jan Laitenberger ([email protected])

module Main (main) where

import CGI
import Maybe(fromMaybe)

main :: IO ()
main = wrapper hello

hello :: [(Name,Value)] -> IO (CgiOut HTML)
hello env =
 return Content {
   mime    =  page "Hello" []
                [ h1 ("Hello " ++ name ++ "!"),
                  gui script [
                    prose ((if null name then "Enter your" else "Change")
                           ++ " name: "),
                    textfield "Name",
                    submit "Submit" " Submit "
                  ] ],
   cookies =  [("Name", name, "/", "Wed, 31-Dec-2004 12:00:00 GMT")]
 }
 where
   name = fromMaybe "" (lookup "Name" env)
   -- NOTE: if like here a cookie and a form field (or query string variable)
   --       have the same name, the form field (qs var) appears first in env.
   --       If you only lookup the first one (that's what I did), the form
   --       (qs content) has priority over the Cookie. To avoid confusion,
   --       it might be a good idea, not to use the same name for Cookies and
   --       other elements.

-- Edit script to point to url of the "executable" of this very module.
script :: String
script = "http://www.janweb.net/cgi-bin/CGI/laitenbe/HelloCookie.hs"

Hosted by www.Geocities.ws

1