Skip to main content

Storing a Cookie

The key work is carried out by the SetCookie method of the %CSP.ResponseOpens in a new tab object. Like the %session and %request objects we used earlier, %response is provided automatically by Caché. It is used to control the server's response to the browser, other than the actual page contents.

Here, we have given the cookie a name of “CacheCinemaLastCategory”, a value equal to the ID of the FilmCategory object associated with the film in the user's order, an expiration date specified by the variable Expires (which we'll define in a moment), and a path of “/”, which makes this cookie available to all Web pages at our site.

ClassMethod CompleteOrder()
    // For each film, update the number of tickets sold
    Set ItemList = 
    Do ItemList.Execute(OrderID)
    While ItemList.Next() {
        Set Film=##class(Cinema.Film).%OpenId(ItemList.Get("Film"))
        Set Film.TicketsSold = Film.TicketsSold 
            + ItemList.Get("AdultTickets") + ItemList.Get("ChildTickets")
        Set ItemID = ItemList.Get("ID")

        Do Film.%Save()
        Set Film = ""
    Set ItemList = ""
    // Put a cookie on the users computer
    Set Item = ##class(Cinema.TicketItem).%OpenId(ItemID)
    Do %response.SetCookie("CacheCinemaLastCategory", 
        Item.Show.Film.Category.%Id(), Expires, "/") 
    Set Item = ""
    // End the session
    Set %session.EndSession = 1
FeedbackOpens in a new tab