Skip to main content

Storing a Cookie

Now, we're ready to send the cookie to the user's browser.

The key work is carried out by the SetCookie method of the %response 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, and a path of “/”, which makes this cookie available to all Web pages at our site.

generated description: handson.gif

—TicketConfirm.OnPreHTTP—
TicketConfirm.OnPreHTTP
ClassMethod OnPreHTTP() As %Boolean
{
    If $data(%session.Data("Order")) {
        Set ord = ##class(Cinema.TicketOrder).%OpenId(%session.Data("Order"))
        Set itm = ord.Items.GetAt(1)
        Set cat = itm.Show.Film.Category.%Id()

        // Put a cookie on the users computer
        Set Expires = +$H + 7
        Set Expires = $ZD(Expires,11) _ ", " _ $ZD(Expires,2) _ " 00:00:00 GMT"
        Do %response.SetCookie("CacheCinemaLastCategory", cat, Expires, "/")
    }

    Quit 1
}
FeedbackOpens in a new tab