SolutionsTools & SDKSupportForums Register



Quick Links
 
Simple Cookie Application
 
 
Techniques for setting, retrieving and deleting cookies without using a digest

Introduction

The UP.SDK includes libraries that will help you include cookies in a digest that is sent to an UP.Browser. However, you may not wish to use a digest, you may wish to set your cookies manually, and you may do this in exactly the same way you would set a cookie when sending an HTML page to a web browser.

This technical note and companion sample application describe how to set, retrieve and delete cookies without using the UP.SDK digest libraries. The application comprises plainCookie.cgi and deleteCookie.cgi. Please try the demo URL to see how they work: http://demo.openwave.com/cookie/cookie.wml.

NOTE: The example code included with this technical note is written in WML 1.1 and Perl, and is portable to any platform with a Perl interpreter.

For more information on setting cookies using the UP.SDK digest libraries, please see Perl Digest Library Utility in the UP.SDK Tools and API Reference Guide.

Let's take a close look at these source codes.

The plainCookie.cgi Source Code

The application is comprised entirely of three cards:

Card 1 which prompts the user to enters their name

Card 2 which confirms the name, and causes a cookie to be set

Card 3 which displays the contents of the cookie

Control of the application is maintained by the values which are returned in the QUERY_STRING HTTP Header. The value of the NEXT variable dictates which card should be returned by the script, and if there is data associated with the "name" variable, then a cookie is set.

The cookie is actually set by including the following line in the HTTP response from the script:

print "Set-Cookie: you=".$cgiVars{"name"}."\n";

This line is called before the the content of the wml deck is actually output to the browser. The cookie information is maintained by the UP.Link (or the UP.Simulator). The contents of the cookie are viewable in the Phone Information window of the UP.Simulator by pressing F11.

The next time the script is invoked (as you navigate to card 3) the cookie is returned to the server along with the HTTP request for card three. The script parses the cookie information with the following code:

@nvpairs=split(/[,;] */, $ENV{'HTTP_COOKIE'});

## To extract the value from a specific cookie:
##
foreach $pair (@nvpairs) {
($name, $value) = split(/=/, $pair);
$cookie{$name} = $value;

and substitutes it in with the third card:

I picked up the cookie you as '. $cookie{"you"} .'

The deleteCookie.cgi Source Code

The application allows you to expire the existing cookie now. When you view this cookie after the specific time, the cookie won't display. The flow of the application is as follows:

  1. catch the current time and convert it to "wday, dd/mm/yy hh:mm:ss GMT" format that is specified in RFC 2109
  2. reset the existing cookie "you" and expire it at the current time print "Set-Cookie: you='deleted'; expires=$day; path=/", "\n";
  3. send a confirmation deck back to you

 
Copyright © 2000-2008 Openwave Systems Inc.    About Us  |  Openwave  |  Terms & Conditions  |  Privacy Policy  |  Update Profile