Lab 13 - Cookies

Overview
In this lab, we'll learn how to store information on the user's browser through the use of HTTP 'cookies'. These are most often used to 'remember' a user between sessions. For instance, when you check the 'remember my login name' checkbox on a web site, you are instructing the server to set a cookie storing your login credentials.

 

Getting Started

Our goal this lab is to make a simple number-guessing game. Your program should pick a random number and then set it as a cookie in the user's browser. It should then show the user a form that lets them guess the value of the number. When they press 'submit', your script should check if their guess is correct and print an appropriate message. If the guess is incorrect, your program should print the form again so that the user can enter another guess.

Do this:
Create a Perl program called numberguess.cgi in your lab13 folder (which you'll need to create, of course). A suggested skeleton for your program follows:
#!/usr/bin/perl -w
use strict;

sub print_header {
    my ($secret) = @_;

}

sub print_guess_form {

}

sub check_guess {
    my ($guess, $secret) = @_;
}

## main

if( param('guess') ) {
  # then we know that the secret number has already been
  # generated, and the user has guessed at least once..
  # do something appropriate.
} else {
  # they've never guessed before.. generate a new secret number.
}

 

Finishing up

Do this: Change your program so that if the user has never guessed before, you a new secret number is generated and passed to the print_header function. This function should set a cookie that stores the secret number.

Now, all that remains is to check for correct guesses...

Do this: Change your program so that if the user has guessed before, the guess is checked against the value of the secret number (which should be stored in a cookie). If the value matches, an appropriate message should be displayed. If not, a different message should be displayed, as well as a form so the user can guess again.

Once you have all that working, you're done!

Submit your assignment

Submit your numberguess.cgi file to the appropriate drop box.

Have a good weekend!

 

 

Thanks to Jacob Ratkiewicz for lab content, Sid Stamm for CSS stylesheets, and Rob Signorelli for original syntax-highlighting JavaScript.