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.
Write a regular expression (slides) that would match the following kinds of patterns. You can use the site Rubular to test your regex.
/^[abcdf][+-]?$/i
/^[acgt]+$/i
/^\d{5}(-\d{4})?$/
/^\d{4}(-?\d{4}){3}$/
/^[-]?\d+(\.\d+)?$/
/^\$[1-9]\d{2,}\.\d{2}$/
/^[a-z]*[aeiou][a-z]*[aeiou][a-z]*$/i
The code for images.php
displays all
JPG images
in the images/
folder.
Modify it using regular expressions so that it will display only image file namess that match each of the following patterns:
(sample solution)
<?php $folder = "images"; $images = glob("$folder/*.jpg"); $regex = "/abbath/i"; # contain "abbath", case insensitive foreach ($images as $image) { if (preg_match($regex, $image)) { ?> <img src="<?= $image ?>" alt="an awesome picture" /> <?php } } # begin with "abbath" and end with "cat", "dog", or "sheep" # $regex = "/^$folder\/abbath(.*)(dog|cat|sheep).jpg$/"; # $regex = "/[0-9].jpg$/"; # end in a number # $regex = "/^[ab]{4}/i"; # start with 4 As/Bs ?>
order.html
allows the user to order a sandwich.
Modify order-submit.php
to use regular expressions to validate the order.
If the order is invalid, kill the page with a brief error message.
(sample solution)
Use a cookie (slides) to count the number of times the user has viewed longcat.php
.
Display this count to the user on the page, e.g. "You have visited 7 time(s)."
(sample solution)
For added challenge:
middle.jpg
once for each page view.
prize.php
is a travel site.
We want roughly every 10th visitor to win a "free trip".
(sample solution)
What day of the month were you born on?
Let's check the box for everyone's day in birthdays.php
.
The form submits to itself, but if you leave the page and come back, it forgets what boxes were checked.
birthdays.php
birthdays.php
(alternate version)
The following page unsafe.php
(source) is a mockup of a badly written user login page.
The programmer implemented the login page poorly because of a misunderstanding about cookies.
Without peeking at the source, figure out the following:
stepp
, reges
, helenem
, and jessica
. The password for stepp
is booyah
but you don't know the others...uwnetid
, password
, and loggedin
.uwnetid
to a name such as reges
and set the cookie loggedin
to true
(such as in Firebug Cookies tab → Cookies → Create Cookie), it will think you are logged in even if you don't know any password.