These are the guidelines for Homework Five. I am following the guidelines for Homework Two. The reason is that that set of notes combines both Homework Four and Homework Two notes. The requirement is that we use Perl/CGI with database support for sessions. So we start like this: #!/usr/bin/perl use CGI; print "Content-type: text/html\n\n"; print qq{
}; Place this in cgi-bin, make it executable, test it. Retrieving the state we need to read from the database. To store the sessions we need to create a table. What's the structure of the table? Seven (5 + 2) columns, called: message, questions, correct, total, key and session_id, modified. http://www.cs.indiana.edu/classes/a348/fall2008/whatsnew/one004.txt The URL above contains the lab notes from Oct. 17, when we developed something similar. Let's go in the right folder: -bash-3.2$ pwd /nobackup/dgerman/mysql-5.0.22 Check if your server is running, if it's not you need to start it. Mine wasn't running so I had to do this: -bash-3.2$ ./step008 -bash-3.2$ Starting mysqld daemon with databases from /nobackup/dgerman/mysql I then connected as a regular user: -bash-3.2$ ./mrbean_connect Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.0.22-log Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> use teddy Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> mysql> show tables; +--------------------+ | Tables_in_teddy | +--------------------+ | example_1013 | | example_1014 | | example_10148pm | | example_1015 | | example_1016 | | friday | | judges | | performers | | ratings | | sampleMidterm | | sampleMidterm_1017 | | transactions | | users | | votes | +--------------------+ 14 rows in set (0.01 sec) mysql> create table hwFive ( -> session_id char(8) primary key, -> message varchar(240), -> questions varchar(240), -> correct int, -> total int, -> anskey varchar(32), -> modified timestamp -> ); Query OK, 0 rows affected (0.03 sec) mysql> show tables; +--------------------+ | Tables_in_teddy | +--------------------+ | example_1013 | | example_1014 | | example_10148pm | | example_1015 | | example_1016 | | friday | | hwFive | | judges | | performers | | ratings | | sampleMidterm | | sampleMidterm_1017 | | transactions | | users | | votes | +--------------------+ 15 rows in set (0.00 sec) mysql> describe hwFive; +------------+--------------+------+-----+-------------------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+-------------------+-------+ | session_id | char(8) | NO | PRI | | | | message | varchar(240) | YES | | NULL | | | questions | varchar(240) | YES | | NULL | | | correct | int(11) | YES | | NULL | | | total | int(11) | YES | | NULL | | | anskey | varchar(32) | YES | | NULL | | | modified | timestamp | YES | | CURRENT_TIMESTAMP | | +------------+--------------+------+-----+-------------------+-------+ 7 rows in set (0.00 sec) mysql> mysql> exit Bye -bash-3.2$ So at this point the sessions table has been created. Here's the first version of our program: #!/usr/bin/perl use CGI; use DBI; use Digest::MD5 qw(md5 md5_hex md5_base64); $DB = "dbi:mysql:teddy:silo.cs.indiana.edu:port=13038"; $username = "mrbean"; $password = "b3an"; $DB_TABLE = "hwFive"; $SECRET = "something secret"; $EXPIRE = 30 * 60 * 60 * 24; # one month $MAX_TRIES = 10; $ID_LENGTH = 8; $q = new CGI; # Open the database -------------------------------------------------------- $DBH = DBI->connect($DB, $username, $password, {PrintError => 0}) || die "Couldn't open database: ", $DBI::errstr; # start the page ----------------------------------------------------------- print $q->header, $q->start_html(-title => 'Database Sessions with URL Rewriting', -bgcolor => 'white'); print qq{ Hello! }, $q->end_html; It connects with the database, then prints Hello! for the user and then quits. You can run this from the command line like so: ./one -bash-3.2$ ./one -bash: ./one: Permission denied I forgot to make the file executable: -bash-3.2$ chmod 700 one Now I can run it: -bash-3.2$ ./one Content-Type: text/html; charset=ISO-8859-1
So next up these two lines are to be added (you know where):
$url = "http://www.cs.indiana.edu/classes/a202-dger/fall2005/notes/flagquiz/images";
@names = ("Australia", "United States", "Russia", "Spain", "Italy", "South Africa", "Brazil", "China");
Then this line:
$questions = join(",", @names);
Then the grading part needs to be started:
if ($key) {
if ($answer eq $key) {
$correct += 1;
} else {
}
$total += 1;
} else {
}
Then we prepare a new question:
($key, $questions) = split(",", $questions, 2);
Then:
$message = "Very good.";
Next:
$message = "No, that was not it.";
Next:
$message .= " Score currently: $correct out of $total.";
Next:
$question = "Whose country is this flag:
"; Printing question: $question End of game situation: if ($questions) { # ... } else { $question = "The game has ended, new game starting, are you ready?
"; $correct = 0; $total = 0; $questions = join(",", @names); $key = ""; # there is no key in the beginning } Then: $message = "Welcome to a new game, score currently: $correct out of $total."; Shuffling will be addressed at the end of these notes. Getting the reset button in this program is achieved through: a) Press to reset the game. b) if ($message && ! $reset) { c) and finally: $reset = $q->param("reset"); End of notes. Throughout the conversion we tested. Here's our test program: #!/usr/bin/perl @names = ("United States", "France", "Spain", "South Africa", "Italy", "Russia", "Brazil"); foreach $name (@names) { print $name, "\n"; } $questions = join(",", @names); ($key, $questions) = split(",", $questions, 2); print qq{ Ask about: $key
Keep ($questions) for later. }; Let's use this program to find a simple solution for shuffling: http://perl.active-venture.com/pod/perlfaq4-dataarrays.html So here's the solution: #!/usr/bin/perl use List::Util 'shuffle'; @names = ("United States", "France", "Spain", "South Africa", "Italy", "Russia", "Brazil"); foreach $name (@names) { print $name, "\n"; } $questions = join(",", @names); ($key, $questions) = split(",", $questions, 2); print qq{ Ask about: $key
Keep ($questions) for later. }; @shuffled = shuffle(@names); print join("-", @names), "\n"; print join("-", @shuffled), "\n"; Let's see this in action a bit: -bash-3.2$ ./one United States France Spain South Africa Italy Russia Brazil Ask about: United States
Keep (France,Spain,South Africa,Italy,Russia,Brazil) for later. United States-France-Spain-South Africa-Italy-Russia-Brazil Russia-Brazil-Italy-Spain-South Africa-France-United States -bash-3.2$ ./one United States France Spain South Africa Italy Russia Brazil Ask about: United States
Keep (France,Spain,South Africa,Italy,Russia,Brazil) for later. United States-France-Spain-South Africa-Italy-Russia-Brazil Spain-Russia-South Africa-France-Brazil-Italy-United States -bash-3.2$ ./one United States France Spain South Africa Italy Russia Brazil Ask about: United States
Keep (France,Spain,South Africa,Italy,Russia,Brazil) for later. United States-France-Spain-South Africa-Italy-Russia-Brazil Italy-Brazil-South Africa-United States-Russia-Spain-France -bash-3.2$ So the last two lines prove that shuffling works well. --