|
How to use Cookies on LiveCode Server The LiveCode Server product brings our English like language to the server environment. The server engine is a separate build of the LiveCode engine with specific syntax and functionality that makes it suitable for use in command-line contexts and, in particular, as a CGI processor. This lesson describes how to process cookies in your LiveCode Server scripts. Code samples are provided IntroductionCookies provide a convenient way to store session specific information client side. Cookies are widely used, and their management is largely down to the configuration of a user's web browser and cookie clean-up policy. The user can enable and disable the support for cookies and remove stored cookies from their machine at any time. Specifying the Cookie DurationAs we discussed in the Introduction, the user can control if cookies are supported and when cookies are to be removed from their system. As a webmaster, this is not a means on which to rely when considering the time a cookie should be available. Cookies can be configured to have a lifespan that is set when the cookie is created by the web page. The following examples demonstrate the creation of two cookies, called "yearcookie" and "sessioncookie": <?lc // Cookie lasts until browser closes Reading CookiesCookies can be read from the user's computer using the $_COOKIE keyword. The following code prints the contents of the cookies that were created in the previous examples to the screen: <?lc put $_COOKIE into tCookiesArray if tCookiesArray is an array then put "<br /><br />" & "FINISHED" Supporting CodeThe code shown so far provides the basic mechanism to save and read cookie content. The array printing command that formats the output of the cookie content is provided here. This code is provided separately in this lesson, as it only serves to present the content in a readable form for this lesson. Normally you would write LiveCode commands to process the cookie content and extract the login information, shopping cart data or other content stored. <?lc command dumpArray pArray, pIndent, pIndentString Output - No CookiesThe first time you run the code listed in this lesson, you may get the following output: PRINTING COOKIES: no cookies found FINISHED This may be because the printing code is executed before you have saved the cookies or because of a latency that exists when writing a cookie to a file system. Output - Cookies FoundIf your code was successful, then you should see the following output in your web browser window: PRINTING COOKIES: $_COOKIE = { FINISHED Both cookie examples are listed and the content of the two cookies is also displayed.
|
|