dcsimg
August 21, 2018
Hot Topics:

Tracking Users Using WML

  • March 27, 2003
  • By Steve Schafer
  • Send Email »
  • More Articles »
Setting a Cookie with Perl

As discussed earlier, we can use the HTTP header Set-Cookie to set cookies. In its simplest usage, this header takes the following form:

Set-Cookie: <name of cookie>=<value of cookie>

For example, a real header might be as follows:

Set-Cookie: name=Steve

When this header is passed to a browser, it sets the cookie "name" equal to "Steve." Because the header doesn't include a time to live (TTL), the cookie is only valid for the current session. When the browser is closed, the cookie expires and is deleted.

To include a TTL, you add the parameter expires as shown in the following example:

Set-Cookie: name=Steve; expires=Monday, 24-Mar-03 23:59:59 GMT

Notice the use of a semicolon (;) to delimit the parameters. The date is in the format "weekday, dd/Mon/yy hh:mm:ss." In the example above, the cookie will expire at one second before midnight on Monday, March 24, 2003, Greenwich Mean Time.

Tip: It's important to include at least one blank line after the Set-Cookie header and before the WML headers so the client correctly identifies the WML headers.

Let's look at a real example of using Perl to set a cookie for a WML deck. The following code snippet shows how Perl is used to set a cookie and output a status ("Cookie set") message:

#!/usr/bin/perl# Define minimal deck$deck = '<wml>  <card>    <p>      Cookie set.    </p>  </card></wml>';# Send Content-type and Set-Cookie headersprint "Content-type: text/vnd.wap.wml \n";print "Set-Cookie: name=Steve \n";# Send WML header infoprint "\n<?xml version=\"1.0\"?>\n";print "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\""   . " \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n";# Send deckprint $deck;

The cookie in the code above was set without a TTL. To set an expiration date, we draw on the Date::Calc module to do our date calculations:

#!/usr/bin/perl# Include Date::Calc.use Date::Calc':all';# Get today in GMT($year,$month,$day) = Today([$gmt]);# Add a year (365 days)($year,$month,$day) =  Add_Delta_Days($year,$month,$day,"365");# Get textual representations of month and day of week$dow =  Day_of_Week_to_Text(Day_of_Week($year,$month,$day));$month = Month_to_Text($month);# Make sure day is two digitsif ($day<10){ $day = '0'.$day;}# Assemble expiration date$date = $dow.", ".$day."-".$month."-".$year." 23:59:59 GMT";# Define deck$deck = '<wml>  <card>    <p>      Cookie set.    </p>  </card></wml>';# Send Content-type and Set-Cookie headersprint "Content-type: text/vnd.wap.wml \n";print "Set-Cookie: name=Steve; expires=$date; \n";# Send WML headersprint "\n<?xml version=\"1.0\"?>\n";print "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\""   . " \"http://www.wapforum.org/DTD/wml_1.1.xml\">\n";# Send the deckprint $deck;

Note: The Date::Calc module has been covered in several previous articles. The module is available from CPAN, at http://search.cpan.org/author/STBEY/Date-Calc-5.3/Calc.pod.





Page 3 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

Thanks for your registration, follow us on our social networks to keep up-to-date