Next / Previous / Contents / TCC Help System / NM Tech homepage

7.14. setCookie(): Set a cookie

This function creates a cookie according to the values in the given userRecord, and then writes to the standard output the header that sets that cookie.

reader.cgi
# - - -   s e t C o o k i e

def setCookie ( userRecord ):
    '''Generate the header to set a cookie.

      [ userRecord is a UserRecord instance ->
          sys.stdout  +:=  header to set a cookie as per userRecord ]
    '''
    #-- 1 --
    # [ cookie  :=  a new Cookie.SimpleCookie() with no morsels ]
    cookie  =  Cookie.SimpleCookie()

For the name of the morsel where the user ID is kept, see Section 7.6.23, “MORSEL_NAME.

reader.cgi
    #-- 2 --
    # [ cookie  +:=  a morsel whose name is MORSEL_NAME and whose
    #                value is userRecord.userId ]
    cookie[MORSEL_NAME]  =  userRecord.userId

For the duration of persistent cookies, see Section 7.6.14, “PERSIST_SECONDS.

reader.cgi
    #-- 3 --
    # [ if userRecord.persist ->
    #     cookie  :=  cookie with its only morsel's maximum age
    #                 set to PERSIST_SECONDS
    #   else -> I ]
    if userRecord.persist:
        morsel  =  cookie[MORSEL_NAME]
        morsel[MAX_AGE]  =  PERSIST_SECONDS

    #-- 4 --
    # [ sys.stdout  +:=  the header that sets cookie=(cookie) ]
    print cookie.output()