October 7 - ndgriffeth/Class-Notes-and-Lectures GitHub Wiki

Assignment for October 15 & 16 (October 14 is Columbus Day)

Reading

For October 9: Read through Chapter 5 (this is the only assignment for October 9, there's no written or programming assignment).
For October 15: Read through Chapter 6.

Programming Assignment due midnight October 15

Registration

Create a form allowing a new user to register at a site. The user should provide a login username, a login password, and a nickname by which he will be known to the site. (You can add other information, but it's not required.)
When a user clicks the submit button, check that the password is at least 8 characters long, contains an upper-case letter, a number, and a special character (anything not a number or letter).
Also, check that s/he gave a non-null username that is not already in the password file. Providing a nickname is optional; you will use the username as the nickname if none is provided. If any of these input validations fails, return a Web page containing the error messages and a suggestion for what the user should do to proceed, together with a link back to the registration page.
Finally, add the username, password, and nickname as a new line in users.txt, which should be in the directory "users," which is a directory below the directory containing the document root. (This is the directory $_SERVER['DOCUMENT_ROOT']."/../users/".)
If you are unable to change the file (check the values returned by fopen and fwrite), return a page requesting the user to try again later.
After a successful registration and update of the file, return the login page (See part 2).
Note that you first need to read the file users.txt into an associative array, in order to check that the username does not already exist. Well, actually, you don't have to read it into an associative array, but I am requiring you to do that and will check your code that you did. This is a little bogus, but I want you to use arrays.

Login

Create a second form allowing a user to login. It should take the login username and password and check them against the file users.txt, making sure that the password matches the password for the user.
Create a page with an appropriate error message if anything doesn't work, and a page welcoming the user to the site if everything works.

Master's Students Projects due midnight October 16

Email me your choice of protocol and two sources that you will use to learn about it. Be specific about the sources (i.e., if a Web page, send the link).

Web Site Project (All Students) due midnight October 16

Email me an initial design of your Web site. You should explain how users will get access to the site and what activities the site will provide for them. Does the site provide products, services, or Internet activities? What are the products, services, or Internet activities? How will the Web site display these to the users? Will there be interactions with other site users? Will there be feedback on how the site worked for a user? Will users be able to contact site administrators with questions? Will money be exchanged, requiring financial information (or at least interaction with PayPal). These are just a few questions to give you some ideas of what you can do. You should also browse sites that you use on the Web to get more ideas.

You should include a list of pages (at least 10 pages) together with a description of the information on each.

HTML5: Form Input Types

http://www.w3schools.com/html/html5_form_input_types.asp

More Array Operations

Sorting

sort: sort an array.
Be careful if it is an associative array; new numeric keys are assigned, using the new sorted order!!
See sortarray1.php and sortarray2.php

asort: sort an array by the values, maintaining the key/value relationships.
Subsequently, if you loop through the array, you get the elements in sorted order by value.
See sortarray3.php

ksort: sort an array by the keys, maintaining the key/value relationships.
Subsequently, if you loop through the array, you get the elements in sorted order by key.
See sortarray3.php

usort: sort with user-defined comparison function.
See the example in the book, pages 94-95. This is nice if you are using arrays in place of a database.

Re-arranging

shuffle: Random re-ordering.

array_reverse: Reverses the order of the elements.

Loading Arrays from Files

file: read a file into an array, so that each array entry is a line of the file.

You could use this to read your saved file:

Traversing Arrays

You can access array elements by manipulating an internal pointer that is associated with every array. Here are the operations for this:

current: return the current value of the internal pointer.
pos: same as current.

reset: set the internal pointer to the first element.
end: set the internal pointer to the last element.
each: return the key,value pair in the current entry and then advance the internal pointer to the next array entry.
next: advance the internal pointer to the next array entry and then return the key,value pair.
prev: return the internal pointer to the previous array entry and then return the key,value pair.

You can also avoid an explicit traversal by applying a function to every element of an array. The function array_walk takes an array as its first parameter and a function name as its second parameter, and applies the function to each array entry.
Compare testarray1.php and testarray7.php

Strings and Pattern Matching

Binary safety

A function is binary safe if it doesn't alter the contents of a string when it operates on it. The basic idea is that it doesn't mess you up by trying to help you. For example, TextEdit displays .html files in a way that prevents you from editing them.

This could be interpreted as meaning that if a function doesn't change its input at all, it's binary safe (so if it processes a string to return a different string, it's binary safe). But that doesn't apply to file read operations -- a file read is binary safe only if the string returned is exactly the string that's stored in the file.

Also, string comparisons don't change strings, but they can interpret strings in different ways to get different results -- a comparison is binary safe if it compares the strings in binary, without interpretation.

Single versus double quotes

When you use single quotes to delimit a string, what you see is what you get. When you use double quotes, variables and various control characters are interpreted. If you are having trouble, try single-quoting and use the concatenation operator to include variables in strings.

Operating on Strings

Checking that fields are filled out

isset: returns true if the variable has a value (other than NULL).

empty: returns true if the variable does not exist or has not been set.

Cleaning up input.

White space is anything that isn't displayed on a page: blanks, tabs, newlines, end-of-string, etc.

trim: strips white space from start and end.
See also: ltrim, rtrim, chop (chop is an alias of rtrim)

Prettying up output.

nl2br: turn newlines into <br />

The following commands can be useful for formatting output, but are complicated and hard to read:

  • printf: prints a formatted string.
  • sprintf: returns the string instead of printing it.
    Everything you can do with these, can be done other ways -- e.g., string concatenation and number_format. I won't test you on these.

These commands let you clean up capitalization:
strtoupper: convert all characters to upper case.
strtolower: convert all characters to lower case.
ucfirst: convert first character of string to upper case.
ucwords: convert first character of each word to upper case.

Utility functions

strlen: get string length

Preparing strings for storing

Certain characters have special meaning in databases: quotation marks, backslashes, and NULL. These characters must be escaped, by putting a backslash in front of them. Commands that help with this:

addslashes: Add slashes to escape the control characters.
stripslashes: Strips backslashes from control characters.

PHP has a "magic quotes" option that can be set in php.ini -- see page 155, figures 4.3 and 4.4 in book. If the magic quotes option is on, PHP "automagically" adds the backslashes to input strings.

Processing strings

explode: breaks apart a string, using a delimiter, and returns an array of strings.
implode: opposite of explode, joining a string with "glue"; join is an alias.
strtok: get substrings (tokens) from a string, one at a time.
substr: returns the substring between start and end positions in a string.

Comparing strings

strcmp: returns 0 if two strings are equal (-1 if the first is less than the second, +1 if the first is greater than the second. Awkward: 0 is interpreted as false in a conditional, so to execute the conditional code when the strings are equal you need "if (!strcmp($s1,$s2)".
strcasecmp: case-insensitive string comparison.
strnatcmp: compares like a human.
strnatcasecmp: case-insensitive "natural" comparison.

See http://sourcefrog.net/projects/natsort/ for more discussion of natural ordering. Natural order comparisons are not binary safe. Case-insensitive comparisons are called binary-safe, but I find it hard to understand why.

Finding and replacing substrings

The following functions find substrings of a string. They return the end of the string, starting with the target substring:

strstr: find the first occurrence of a substring; strchr is an alias.
stristr: find the first occurrence of a substring, using case-insensitive matching.
strrchr: find the last occurrence of a substring.

The following functions are similar, but they return the position of the substring in the string:

strpos: returns the position of the first occurrence of a substring in a string.
stripos: same as above, but case-insensitive.
strrpos: returns the position of the last occurrence of a substring in a string.

The following functions add "replace" functionality:

str_replace: replaces all occurrences of a search substring with a replace substring.
substr_replace: replaces the substring in a given position with the replacement substring.

Pattern Matching with Regular Expressions

ereg: Match a string to a pattern, returns the length of the matched string (if found) or FALSE if not. Since this is what the book covers, this is what we will do.

Deprecated!! For the current alternative, see http://www.php.net/manual/en/book.pcre.php.

Simple matching

Matches the way strstr matches. For example, "regular" matches "re", "ula", "egul", and so on. The integer returned is 2, 3, and 4, respectively (the length of the match).

Matching with character sets

. is the wildcard, and stands for any character [a-z] stands for any lower-case letter. You can express any range by giving the first and last characters in this way. [aeiou] stands for any vowel. You can express any set as a list this way. Special sets are given in table 4.3, page 125, in the textbook.

Anchoring the match

You can force a match to the beginning or end of a string using "^" for the beginning and "$" for the end.

Repetition

  • means the preceding expression occurs at least once (and possibly many times)
  • means the preceding expression occurs 0 or more times ? means the preceding expression occurs 0 or 1 times (it may or may not occur)

More flexibly, you can use {m,n} to say the preceding expression occurs at least m and at most n times.

To apply the above to an expression with more than one character in it, you need to use parentheses.

The escape character

So far, we have seen a number of characters with special meanings in a regular expression: +, *, ?, ^, $, {, }, [, ], (, ) (see also table 4.4, page 128). To match one of these characters, you use the backslash as an escape (which means that to match a backslash, you escape it with a backslash).

The "or" operator

You can try to match multiple regular expressions by combining them with a "|" -- the "or" or "branching" operator.

Exercises

A regular expression that matches

  • the substring abc: abc
  • any numeric decimal string: [0-9]+
  • any combination of a's, b's, and c's: [abc]+
  • any combination of a's, b's, and c's of length 3 or greater: [abc]{3,3}[abc]*
  • any combination of a's, b's, and c's of length less than 3: [abc]{0,2}
  • a valid decimal integer: [0-9]+
  • a valid rational number (i.e, it can have a decimal point): ([0-9]{1,3}\,([0-9]{3,3]\,)*([0-9]{3,3})(\.[0-9]*)?) | ([0-9]{1,3}(.[0-9]*)?)
  • a person's name
  • a valid number in scientific notation
  • a valid date in mm-dd-yyyy format
  • an email address (as discussed in class): [a-zA-Z0-9_][a-zA-Z0-9_\.] * @ ([a-zA-Z0-9]+\.){1,2}[a-zA-Z]+

Review Questions

  1. Given an array $myarray having the following value, what does each of the following statements do:
Key Value
alpha 10
beta 1
gamma 100

a. sort($myarray);
b. ksort($myarray);
c. asort($myarray);

  1. Define a regular expression for any of the exercises above.

  2. What values can each of the following functions return:
    a. fopen.
    b. fgets.
    c. fwrite.
    d. explode.
    e. strcmp.
    f. strlen.

  3. Write a loop to read a file of usernames and passwords, and build an associative array in which the key is the username and the value is the password.

⚠️ **GitHub.com Fallback** ⚠️