Lab 4: More PHP
Introduction
This week you will improve and augment the server side processing you started in the previous lab by improving data validation, improving data storage, and creating reports.
Read the entire lab so you see the requirements and know what is coming.
Part 0 - Setting up (5%)
- Directory:
- You must create a folder in your public_html/IT350 folder called Lab04 and store your work in that directory. To run a script called yourfile.php stored in your Lab04 directory on public_html/IT350, type http://midn.cs.usna.edu/~mXXXXXX/IT350/Lab04/yourfile.php in the address bar of your browser (replace XXXXXX with your alpha).
- You should COPY your files from Lab03 into this directory and make changes to them here! See the figure at the end of this lab regarding where files should be. Make sure you do not modify the files in previous labs folders. Make sure files in previous labs folders are not open in your editor!
- Delete the LOG.txt file in your new Lab04 folder. The file will be created again when the submit.php script you will modify for this lab runs.
- IMPORTANT: To allow the webserver to later create files when running a PHP script, the webserver user might need to have extra permissions on your Lab04 directory. To enable this, ssh into midn.cs.usna.edu. You can use putty or some other tool if you are on a Windows machine. Use your normal USNA credentials. Type the following in the window that appears:
cd ~/public_html/IT350/ chmod -R a+rwx Lab04
Part 1 - Enhancements (50%)
- Validity check: Modify submit.php to add another validation check: add a check that involves pattern matching using regular expressions to verify that the password contains at least one lower case letter, one upper case letter, and one digit. If the password does not have the required strength, your script should state explicitly what the error was, and allow the user to submit again.
- Logging: Modify your code (submit.php) so that it records all of the form fields (including the password) in a file called
LOG.txt as before, but with some modifications:
- First time when the file is created, your program writes a header line as the first line, which contains a meaningful heading name for each variable that will be stored. The heading names should be tab separated, as the values will be.
- As before, you will want to append to this file (so it contains a history of everything that has happened). If and only if the values pass your 'Validity' tests, then write all the form's parameters provided by the user to LOG.txt. The values provided by a single user should all be on a single line. Use a tab ('\t') as a separator.
- Make sure that before writting to file, you replace any newline characters that the user might have submitted (in their textarea field) with some special string, for example "&&" so all the input values from a user are indeed on one line in the file.
- Make sure all the values the user selects using checkboxes are saved to the file - these values should NOT be separated by tabs, but by another character (e.g. comma "," or pipe "|"). See hint in the previous lab about how to name the checkboxes such that the values selected by the user are stored in a PHP array.
Part 2 - Reports (95%)
For this part you will write a new form requestReport.html and a new CGI program (in PHP) createReport.php that reads your LOG file and generates a summary report of the submissions based on the selection criteria chosen in the requestReport.html. Be sure that your LOG.txt has enough data in it to make this report at least a little interesting.- requestReport.html: Create a new form in requestReport.html that allows a potential administrator of your website to select the value of some criteria for generating reports. The form should have at least one input field that allows the potential admin to select or specify the value for the criteria of the report. You decide the criteria, the admin will select/specify a value. Create a form that makes most sense for your website. For example, you can allow the admin to request a report with all users that selected a given checkbox, or selected a given radio button, or had a particular value in an input field (from your registration.html form). Again, you (as developer) decide the criteria, the admin will get to choose the value. The action for the form should be createReport.php.
- createReport.php: Create a new PHP script, createReport.php, that takes the input provided by the potential admin in requestReport.html and displays a table with all the data in LOG.txt that satisfies the conditions specified in the input. The report should satisfy the following conditions:
- Only the lines from LOG.txt that satisfy the condition specified in requestReport.html will be included in the report
- The user data that will be displayed is sorted based on some order that makes sense for your website (e.g. by last name, workshop selected, etc - something in registration.html). You decide the order. See the usort, sort, or asort functions for how to sort your data
- The report has a summary line that specifies how many user submissions are in the report, what is the criteria for selection (based on the admin input selection for the report), and a description of the sorting order of the data displayed (e.g. "Below are the 5 users registered for the "Get to know you" picnic, sorted by last name")
- All user data is displayed (passwords should be masked with **) in a table format
- The table displaying the user data has headers with values read from LOG.txt's first line
Challenge Problem - Just a bit more (100%)
- Create a utilFunctions.inc.php file with functions you could be using for your website: e.g. to validate input, write to file, read from file, sort, create table from an array, display HTML page given title and content, etc. Modify your submit.php and createReport.php to use the functions from utilFunctions.inc.php.
General Requirements and Deliverables
- Documentation: ensure you have appropriate comments in your PHP scripts.
- Links: Create three links in your top-level IT350.html page
under the heading 'Lab04'
- Under the name 'Form', make a link to your Lab04 registration form page (Lab04/registration.html)
- Under the name 'Report', make a link to your Lab04 report form page (Lab04/requestReport.html)
- Under the name 'LOG.txt' make a link to your log file (Lab04/LOG.txt)
-
Always put a README.txt file in the lab directory. At a minimum this file should have:
- *Lab number
- *Your name and alpha
- *Collaborations in completing this lab (people, online sources used outside the course website)
- *How far you got (through which part did you finish)? If you tried any additional parts beyond the one you completed, what did you do?
- *How long this lab took you
- Any suggestions to improve the lab
- Any comments needed for the instructor to review the lab (usernames, passwords, etc.)
- The file displaying the report form for this lab should be called requestReport.html.
- Your PHP file that creates the report should be called createReport.php
- All of your files should be in a folder called Lab04 in your public_html/IT350 folder. Your instructor will assume that your web pages are viewable at http://midn.cs.usna.edu/~mXXXXXX/IT350/Lab04/registration.html where XXXXXX is your alpha number.
- All labs must be complete and saved to the midn.cs.usna.edu drive before you submit your assignment. Do NOT modify your web files after you have submitted your assignment (unless you resubmit, which you can do as often as you like up until the deadline.)
-
Submit all files to the online submissions system (submit.moboard.com)
on or before the due date, this is Lab04. Use the command line submit script and
capture the entire contents of the directory. The easiest way to do this would be to cut and paste the following
while logged onto the webserver:
This assumes that the submit script is located in ~/bin/ and is executable.cd ~/public_html/IT350 ~/bin/submit -c=IT350 -p=Lab04 Lab04
When finished, the structure of your web site should look like this: (items in blue undergo modification during this lab)
\\midn.cs.usna.edu\mXXXXXX\public_html\IT350
IT350.html (main page with links to all labs)
images/(images folder might optionally change for this lab)
Lab01/
index.html
Lab02/
index.html
schedule.html
registration.html
Lab03/
index.html
schedule.html
registration.html
submit.php
LOG.txt
Lab04/
index.html (the main page for your unit/ECA copied from Lab03)
schedule.html (copied from Lab03)
registration.html (copied from Lab03 and potentially modified)
submit.php (copied from Lab03 and modified for this lab)
LOG.txt (created and updated by the webserver when submit.php is invoked)
requestReport.html (new form created for this lab)
createReport.php (new PHP script created for this lab)
Additional Hints/Clarifications
- If your code is not working, read the error messages in the Apache log file.
Here is a list of PHP errors and the likely solutions: PHP errors explainedssh midn.cs.usna.edu tail -f /var/log/apache2/error.log | grep mXXXXXX
If you have logical errors in your program, try adding extra echo commands to see what parts of the program are executing and what the values received by the prgram are.