20070323a

From Wsms

Jump to: navigation, search

previous next
GO TO:
Parent class notes: PHP class notes
Angelica's complementary notes



Friday March 23 2007

Contents

Quiz 29

See: http://wsms.wikiplanet.com/html/quiz/quiz29_20070323.html

Question 1

( points) You are in the Internet Ice Cream business. Design a form that requests the user's first name and presents the user with these choices:
The user may choose from three flavors of ice cream: Chocolate, Strawberry, Vanilla.
The user may choose to order 1, 2 or 3 scoops.

You can compute the cost of the ice cream by the formula:
cost = (#Scoops X .95) + tax
where the tax is 7.75%

Present the user with a screen that greets them by name, verifies the flavor they ordered and tells them the cost in the form $9.99.

George's answer

See:
http://rop.ncc.sdccd.net/~georgeg/php/20070323/01-htm.txt
http://rop.ncc.sdccd.net/~georgeg/php/20070323/01-php.txt

Joe's answer

For the selections it makes sense to use either radio buttons or a drop down list. I used the drop down list; Joe used radio buttons. See http://rop.ncc.sdccd.net/~georgeg/php/20070323/JoesAnswers/

Question 2

( points) Your hobby is gardening. You are preparing a form for users to complete when they sign your guestbook. Present the user with a form that allows them to enter:
Their Name
Email Address
Their Favorite flower

Present the user with a confirmation screen that shows the data entered, but make certain that all data fields are "cleansed" as follows:

There are no extraneous leading or trailing blanks.
If the user has entered the work "curseword" it has been replaced by the string "?????."
Any html tags have been removed.

See:
http://rop.ncc.sdccd.net/~georgeg/php/20070323/02-htm.txt
http://rop.ncc.sdccd.net/~georgeg/php/20070323/02-php.txt

Miscellany

Validating data

In the real world it is important to validate data that comes from users to prevent things like cross-site scripting and sql injection. It is also important to have data in a normalized form if you are going to stick it in a database. See http://php.net/trim See http://pear.php.net/package/Validate.

Encrypting Data

One-way encryption good for passwords. http://php.net/crpyt

Not in the manual? http://php.net/encrypt

You can see from http://rop.ncc.sdccd.net/~georgeg/php/info.php, that mcrypt is disabled. It's also disabled on our Windows servers.

Moving right along...

Validating data with PEAR Validate

See: http://www.phpbuilder.com/columns/ian_gilfillan20060630.php3?page=2&print_mode=1

Setting up support for validate is a little difficult. You need to set up your own pear library, etc. See: http://pear.php.net/manual/en/installation.shared.php

[georgeg@rop ~]$ pear config-create /home/georgeg .pearrc
Configuration (channel pear.php.net):
=====================================
Auto-discover new Channels     auto_discover    <not set>
Default Channel                default_channel  pear.php.net
HTTP Proxy Server Address      http_proxy       <not set>
PEAR server [DEPRECATED]       master_server    <not set>
Default Channel Mirror         preferred_mirror <not set>
Remote Configuration File      remote_config    <not set>
PEAR executables directory     bin_dir          /home/georgeg/pear
PEAR documentation directory   doc_dir          /home/georgeg/pear/docs
PHP extension directory        ext_dir          /home/georgeg/pear/ext
PEAR directory                 php_dir          /home/georgeg/pear/php
PEAR Installer cache directory cache_dir        /home/georgeg/pear/cache
PEAR data directory            data_dir         /home/georgeg/pear/data
PHP CLI/CGI binary             php_bin          <not set>
PEAR test directory            test_dir         /home/georgeg/pear/tests
Cache TimeToLive               cache_ttl        <not set>
Preferred Package State        preferred_state  <not set>
Unix file mask                 umask            <not set>
Debug Log Level                verbose          <not set>
PEAR password (for             password         <not set>
maintainers)
Signature Handling Program     sig_bin          <not set>
Signature Key Directory        sig_keydir       <not set>
Signature Key Id               sig_keyid        <not set>
Package Signature Type         sig_type         <not set>
PEAR username (for             username         <not set>
maintainers)
User Configuration File        Filename         /home/georgeg/.pearrc
System Configuration File      Filename         #no#system#config#
Successfully created default configuration file "/home/georgeg/.pearrc"

Put the following in bash profile:

PATH=~/pear/bin:$PATH:$HOME/bin

Log out and in again to verify that your change to .bash_profile works.

[georgeg@rop ~]$ echo $PATH
/home/georgeg/pear/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/georgeg/bin

Intall pear

[ggeller@ws05 ~]$ pear install -o PEAR
...

Install the Validate package:

[georgeg@rop ~]$ pear install --alldeps channel://pear.php.net/Validate-0.7.0
/tmp/glibctestuqMWI6:1:22: error: features.h: No such file or directory
downloading Validate-0.7.0.tgz ...
Starting to download Validate-0.7.0.tgz (16,841 bytes)
......done: 16,841 bytes
downloading Date-1.4.7.tgz ...
Starting to download Date-1.4.7.tgz (55,754 bytes)
...done: 55,754 bytes
install ok: channel://pear.php.net/Date-1.4.7
install ok: channel://pear.php.net/Validate-0.7.0

Make some code like this:

set_include_path('/home/georgeg/pear/php/' . PATH_SEPARATOR . get_include_path());
$old_err = error_reporting(E_ERROR);
require_once 'Validate.php';
error_reporting($old_err);
...
$validate = new Validate();
if ($validate->email("$email")) {
  echo "Valid email<br />";
 } else {
  echo "Invalid email<br />";
 }
...

See the code at: http://rop.ncc.sdccd.net/~georgeg/php/20070323/validate/02-php.txt
ane http://rop.ncc.sdccd.net/~georgeg/php/20070323/validate/02-htm.txt
See it in actions at: http://rop.ncc.sdccd.net/~georgeg/php/20070323/validate/02.htm

Personal tools