* Demonstrates the use of Cookies to preserve session information * Begin %rc is Float %sp is Float %fp is Float %dt is Float %i is Float %Checked is String len 10 %s is String len 50 %FPref is String len 50 %rc = $Web_Sub('@@') %rc = $Web_Type('TEXT/HTML') * Create a list of options to be shown to the user later. * ($ListNew and $ListAdd are Sirius "list" functions that create * easily maniplated data structures as an alternative to %arrays.) %sp = $ListNew %rc = $ListAdd( %sp, 'Aisle') %rc = $ListAdd( %sp, 'Window') %rc = $ListAdd( %sp, 'Below overstuffed overhead bin') %rc = $ListAdd( %sp, 'Next to turbine intake') %rc = $ListAdd( %sp, 'Near distressed infant') %FP = $ListNew %rc = $ListAdd( %FP, 'Superheated Starch Globules') %rc = $ListAdd( %FP, 'Mystery Meat Amalgam (Considered flight insurance?)') %rc = $ListAdd( %FP, 'Heavily salted snack') %rc = $ListAdd( %FP, 'Petrified omelette') In JANWEB Include WEB/TEMPLATE_TOP Print '

Strand Travel Customer Preference Form

' * Web form processing typically calls for execution of two passes * through the program processing the form. The first is to display the * form and await input. After putting up the form, the program terminates * and awaits being called again as a result of the user filling in and * submitting the form. The second pass processes the form input and * formats and presents the data in response to the user's request. * How to know which process is intended in a given call to the program? * A standard http "header" value is passed from the browser to the * program with the name "method" and the value is either "post" or "get". * Think of GET as "get the form", so that pass would present the form. * Think of POST as "post the results", so that pass would process it. * Test which pass this is. If form processing, jump to there. If $Web_Hdr_Parm('METHOD') eq 'POST' Then Jump to ProcessForm End If * Begin building the page to be presented to the user to request input. * Note, however, that this is a program for handling cookies. This means * that the user may have previously executed the program (both get and * post portions) and as a result may have had some cookie data stored * in their browser's cookie file to hold their preferences. * If so, we will welcome them back and show their preferences, otherwise * we'll present the form as if they are a first time visitor. * When the cookie is stored (as will be described later) it will be setup * so that whenever this program is called again, the cookie will be * passed to the program for inspection and/or processing. * We can quickly tell if a cookie has been stored (and is being passed * to the program) by way of WEB_NUM_COOKIE which tells us the number of * cookies being passed to the program. If $Web_Num_Cookie ne 0 Then Print 'Welcome back. Your preferences are noted below.

' Else Print 'Welcome to Strand Travel. Please indicate your preferences below.

' End If * The

tag sets up a form with an action that tells it to * execute this program again on the user's pressing of Submit. Html

Enter your seating preference:

' Print '

Enter your food preference:

' Print '' * The following line creates the Submit button, a standard part of forms. Print '' * Now we want to decide what sort of info to present to the user, * depending on whether this execution of the "form display" part of the * program is after a cookie is set or not. * If not, they're a first time user and we want to explain what the * whole demo is about. * If they've been through once and have had a cookie set, we want to * present them information that talks about the results of having set * the cookie. If $Web_Num_Cookie eq 0 Then Html

This form demonstrates the use of HTTP Cookies.

Cookies are an extension to the HTTP protocol that allow HTTP servers to store a client's preferences and selections on the client machine.

If this is your first visit to this page today, none of the preferences are checked. Click an entry in the seat preferences, then check a box or two in the food preferences.

Now submit the form.

The server processes the the form, and sends your preferences back to your browser as HTTP Cookies.

The next time you request this form, your earlier preferences will be sent back to the server in a "cookie". The server application uses the cookies to fill in the form with your earlier choices.

Else

This time, the data you entered has been retrieved from the cookie which was stored when you submitted your preferences.

You may change your preferences again if you like, and repeat the process.

End Html End If Html

You can see the User Language code' that does all this.

End Html * We're done. End the program which will cause the form to be displayed. Jump to FormDone PROCESSFORM: * Begin processing the form input. We jump to here if the top of the * program has determined that we are to process form input (method=post). * First, we should prepare for setting the 2 cookies which will hold * the selected preferences (one for seating, one for food). Note that * when setting cookies, we provide need to provide a name for the cookie, * a value for it, and an expiration date/time. This date/time tells the * browser when it can remove the cookie from its cookie file. The * date/time value must be in a special binary format. Fortunately, * $WEB_DATE returns the current date in this format. We'll add 24 hours * to it so that the cookie will expire tomorrow. %dt = $Web_Date %dt = %dt + (60*60) * When processing a form, the input that the user selected or entered is * returned as "form parms". If we know the names of these parms, we can * simply query what value (if any) has been entered. If a radio button * was selected, the chosen one is stored as the value of that form parm * (again, think of them as field,value pairs). %s = $Web_Form_Parm('SEATPREF') If %s eq '' Then Print '

Seat preference missing. SEATPREF cookie not stored.

' Else %rc = $Web_Set_Cookie('SEATPREF', %s, %dt) If %rc ne 0 Then Print '

Error storing cookie SEATPREF!' * else * Print 'Cookie stored with name=SEATPREF and value=' with %s End If End If * The preferences for food were offered in check-box-format. This means * the user may select multiple values. In this case, multiple occurrences * of the form parm named "foodpref" may exist, each with the value of the * preference chosen. We need to loop through those. We'll accumulate them * in a string of comma separated values and also store them in the cookie * that way. FOR %I FROM 1 TO $WEB_NUM_FORM('FOODPREF') %FPREF = %FPREF WITH ',' WITH $WEB_FORM_PARM('FOODPREF', %I ) END FOR %rc = $Web_Set_Cookie('FOODPREF',%FPref,%dt,'/') If %rc ne 0 Then Print '

Error storing cookie FOODPREF!' *Else * Print '

Cookie stored with name=FOODPREF and value=' WITH %FPref END IF * We're done processing the form. * Note, however, that we did not put the form out on this pass through * the program. We could have done that, but we instead are putting out * a "thanks for your input" type display. Since we're explaining to * programmers how this all works, we offer directions on going back * and witnessing the results of viewing the "get" pass with cookies * processed. (The difference will be the different welcome message and * explanation of what happened, offered above.) Html

Thanks for using Strand Travel

Your preferences will be stored for 1 day.

Now, return to the previous page (using your browser's) back button.

Once there, issue your browser's Reload or Refresh command to simulate your returning to the web page at a later point in time (otherwise, you're just looking at the very same screen you just submitted.)

You'll notice that the welcome message changes, and there will now be an explanation of how the cookie worked.

 
End Html FormDone: In JANWEB Include WEB/TEMPLATE_BOTTOM End