"); } else { $mime_type = "text/html"; header("Content-type: $mime_type; charset=utf-8"); } ?> CS-90.3 Assignment 9

CS-90.3 Assignment 9

Requirement

Add functionality to Assignment 8 so that the response to submitting the form is one of three messages:

The Assignment

  1. Prepare your database.

    Use a spreadsheet program such as OpenOffice Calc, Microsoft Excel, or Apple Numbers to create a table with two columns. Put a list of email addresses in the first column, and a set of corresponding passwords in the second column. You may use real email addresses, but be sure the passwords are all fictional! Include 4-5 rows in your table.

    Create a directory named db in your web site, and save your spreadsheet there as a CSV (comma separated values) document.

  2. Use PHP to display your database.

    Use the PHP fopen() function to open your database file, and then use a while loop to read each line in the file using the fgetcsv() function, generating a table row in your web page for each line in the file.

    • Open the file. Save the value returned by fopen() in a meaningfully-named variable, such as $user_database_connection. I am purposely using a long variable name as an example to encourage you to think of a name that would be equally meaningful but easier to type!
    • Here is how to read lines from a file until they all have been read:
      while ($row = fgetcsv($user_database_connection)) { // Process each line from the file here }

      Your job is to replace the comment line in the above sample code with PHP statements that will echo a table row with two cells in it, which you are to fill with the values of $row[0] and $row[1].

      Be sure your page generate the proper start tag for the table before the loop, and the proper end tag after the end of it. There should also be a header row before the first data row.

      Use CSS to style the table so it is easy to read.

  3. Implement the Assignment.

    Now that you have established that you can process the information in the database file, use if, else if, and else to implement the following algorithm:

    • Set a variable (conventionally named, $found) to false.
    • Reset the database file to the beginning. After going through the loop that generates the table of database contents, you can reposition the file so you can read it all over again by using the fseek() function like this: fseek($user_database_connection, 0);
    • Loop through the lines in the database file, comparing the first element to the value of the username from the form. If there is a match:
      • Set $found to true.
      • If the password value from the form matches the second element from the database row, generate the third message (“Welcome… ”), and use a break statement to exit from the loop. Fill in the blank with the user’s name.

        Optional: You can use the === operator to compare strings for equality, but this is a case-sensitive comparison. If you want to let the user name be case-insensitive, the easiest way is to use PHP’s strcasecmp() function, which returns the value 0 if two strings passed as arguments are equal, ignoring upper/lower case differences. Do not use this function for checking passwords!

      • If the password value does not match the second element from the database row, generate the second message listed above. Again, use a break statment to exit the loop. Fill in the first blank with the name of the user submitted from the form, the second blank with the password submitted from the form, and the third blank with the value of the second value in the row from the database.
    • After the end of the loop, test the value of $found: if it is not true, echo the first message above. Fill in the blank with the user name submitted from the form.
  4. Use CSS to make everything beautiful!

Submit

When you are sure your program does everything correctly, and that there are no errors are warnings from the XHTML and CSS validators, nor from Firebug, just send me an email message, and I’ll check it out in the usual way.

Send your email message to me at: vickeryatbabbage.cs.qc.cuny.edu by midnight of the due date.

The Subject line of your email message must be: “CS-90.3 Assignment 9.”

Don’t forget to put your name in your email message!