Except where otherwise noted, the contents of this document are Copyright © Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
thanks to former TAs Victoria Kirst, Jeff Prouty, Morgan Doocy, Brian Le for their work on these labs.
This week, we'll write code to allow a student to submit a credit card number in exchange for a good grade in this class. (Just a joke!)
The HTML page buyagrade.html
will have a form that submits to sucker.php
. Then sucker.php
will output a web page to confirms that the student's money was taken.
Download the HTML file below (right-click, Save Target As...) to get started:
(See example screenshot on next slide.)
Let's create an HTML form that POSTs its submitted data to a PHP program on a server.
buyagrade.html
. Upload it to Webster and point your web browser to it on Webster.
name
attributes to the form controls so they will be sent as query parameters; the names are up to you.
value
attributes.
action
attribute to:
http://webster.cs.washington.edu/params.php
Your page should look like this. You can also try our runnable solution (don't peek at the source!).
(See example screenshot on next slide.)
Now write the PHP page to handle the submitted form data. Start from the skeleton above.
buyagrade.html
to POST its data to sucker.php
.
sucker.php
to display the name, credit card number and type (Visa or MasterCard).
<?= $foo ?>
.
Your page should look like this. You can also try our runnable solution (don't peek at the source!).
(See next two slides for example screenshot and file permission issues.)
Modify your sucker.php
page to save the submitted data to a file suckers.txt
.
Ryan;MA;1234123412341234;visa Kevin W;MF;5963109385987345;mastercard Kimberly Todd;MC;7328904328904902;mastercard Marty Stepp;MC;4444100020003000;visa
<pre>
element to preserve whitespace.
file_get_contents
and file_put_contents
.
If you see an error about "Permission denied", right-click the suckers.txt
file in FileZilla and choose File Permissions....
In the box that appears, check the permission boxes for "Write" permission.
Do the same for your lab4
folder itself; and also give the lab4
folder the "Execute" permission.
Your page should look like this. You can also try our runnable solution (don't peek at the source!).
(See example screenshot on next slide.)
Update sucker.php
to verify that all parameters exist and that the user did not leave any blank.
isset
.
$_GET
or $_POST
is blank by comparing it to the empty string, ""
.
buyagrade.html
page.
Your error message should look like this. You can also try our runnable solution (don't peek at the source!).
(See example screenshot on next slide.)
Update your sucker.php
file to further check the validity of the credit card number:
Your error message should look like this. You can also try our runnable solution (don't peek at the source!).
Update your sucker.php
file to check credit card numbers with the classic Luhn checksum algorithm.
from Building Java Programs:
A valid credit card number passes a digit-sum test known as the Luhn checksum algorithm. Luhn's algorithm states that if you sum the digits of the number in a certain way, the total sum must be a multiple of 10 for a valid number. Systems that accept credit cards perform a Luhn test before contacting the credit card company for final verification.
The algorithm for summing the digits is the following: Consider each digit of the credit card to have a zero-based index: the first is at index 0, and the last is at index 15. Start from the rightmost digit (index 15) and process each digit one at a time. For digits at odd-numbered indexes (15, 13, etc.), simply add that digit to the cumulative sum. For digits at even-numbered indexes (14, 12, etc), double the digit's value, then if that doubled value is less than 10, add it to the sum. If the doubled number is 10 or greater, add each of its digits separately into the sum.
from Building Java Programs:
sum = 0. for (each digit of CC number, starting from the last index): if index is odd: add digit to sum. else: double the digit's value. if doubled value < 10: add doubled value to sum. else: split doubled value into its two digits. add first digit to sum. add second digit to sum.
If you finish all the exercises, you can add any other content or code you like to your page.
If the lab is over or almost over, check with a TA and you may be able to be dismissed.
Great work!