Title: afea 1
1?????aµµat?sµ?? ??ad??t???
LECTURE 7
Managing Multiple-Form Applications Writing
scripts with multiple screens
??. Ge?????? F. F?a???????
2Objectives
- To understand what are multiple-form Web sessions
- To learn how to use hidden fields to build
multiple-form user sessions - To learn how to use browser cookies to track data
about the user - To learn how to use PHP session functions and how
to use them to track data about the user
3What Are Multiple-Form Web Sessions?
- A multiple-form Web session leads the user
through a series of HTML forms that work together
and pass data from form to form. - E.g., a shopping cart or on-line survey.
4Example Multiple Screen Session
5Using Hidden Fields to Pass Data
- Hidden fields are part of HTML forms
- Not displayed but value can be accessed in
receiving script like any other variable. - Can still be viewed by users who view source.
6A Full Script Example
- Consider an example script sets a hidden field
- Implements the Order Info form
- on submit sends data to http//webwizard.aw.com/p
hppgm/C7/order2.php
7PHP Script
- 1. lthtmlgtltheadgtlttitlegtOrder Productlt/titlegtlt/headgt
ltbodygt - 2. ltform action"http//webwizard.aw.com/phppgm/C
7/order2.php" - method"post"gt
- 3. ltfont color"blue" size"5"gt Happy Harry's
Hardware Product - Order Formlt/fontgt
- 4. ltbrgtltfont color"red" size"4"gt
- 5. We have hammers, handsaws, and wrenches on
special today! - 6. lt/fontgt
- 7. ltinput type"hidden" name"sample_hidden"
value"Welcome!"gt - 8. ltbrgtEnter Item ltinput text type"text"
size"15 maxlength"20" name"product"gt - 9. Enter Quantity ltinput text type"text"
size"15 maxlength"20" name"quantity"gtltbrgt - 10. ltbrgtltinput type"submit" value"Click To
Submit"gt - 11. ltinput type "reset" value"Reset"gt
- 12. lt/formgtlt/bodygtlt/htmlgt
8 The Output ...
- The previous code can be executed at
http//webwizard.aw.com/phppgm/C7/order.html
9Receiving Hidden Fields in Web Sessions
- Your scripts can receive data from hidden fields
like any other data. - Suppose the following is stored at
http//webwizard.aw.com/phppgm/C7/order2.php
10Receiving PHP Script
- 1. lthtmlgtltheadgtlttitlegt Order Product 2 lt/titlegt
lt/headgt - 2. ltbodygt
- 3. ltform action"http//webwizard.aw.com/phppgm/C
7/order3.php method"post"gt - 4. lt?php
- 5. print "ltfont size5 colorbluegt"
- 6. print "Hidden valuesample_hidden
ltbrgtlt/fontgt" - 7. print "You selected productproduct and
quantityquantity" - 8. print "ltbrgtltbrgtltinput type\"hidden\"
name\"product\ value\"product\"gt " - 9. print "ltinput type\"hidden\"
name\"quantity\ value\"quantity\"gt" - 10. print "ltinput type\"hidden\"
name\"sample_hidden\"value\"sample_hidden\"gt" - 11. print 'Please enter your name'
- 12. print 'ltinput type"text" size"15"
maxlength"20 name"name"gt' - 13. print ' and billing code (5 digits)'
- 14. print 'ltinput type"text" size"5"
maxlength"5" - name"code"gt'
- 15. print 'ltbrgt ltinput typesubmit value"Process
Order"gt' - 16. print 'ltinput typeresetgt'
- 17. ?gtlt/formgtlt/bodygtlt/htmlgt
11Receiving PHP Script With REGISTER_GLOBALS Off
- 1. lthtmlgtltheadgtlttitlegt Order Product 2 lt/titlegt
lt/headgt - 2. ltbodygt
- 3. ltform action"http//webwizard.aw.com/phppgm/C
7/order3.php method"post"gt - 4. lt?php sample_hidden _POSTsample_hidden
- 5. product _POSTproduct quantity
_POSTquantity - 5. print "ltfont size5 colorbluegt"
- 6. print "Hidden valuesample_hidden
ltbrgtlt/fontgt" - 7. print "You selected productproduct and
quantityquantity" - 8. print "ltbrgtltbrgtltinput type\"hidden\"
name\"product\ value\"product\"gt " - 9. print "ltinput type\"hidden\"
name\"quantity\ value\"quantity\"gt" - 10. print "ltinput type\"hidden\"
name\"sample_hidden\"value\"sample_hidden\"gt" - 11. print 'Please enter your name'
- 12. print 'ltinput type"text" size"15"
maxlength"20 name"name"gt' - 13. print ' and billing code (5 digits)'
- 14. print 'ltinput type"text" size"5"
maxlength"5" - name"code"gt'
- 15. print 'ltbrgt ltinput typesubmit value"Process
Order"gt' - 16. print 'ltinput typeresetgt'
- 17. ?gtlt/formgtlt/bodygtlt/htmlgt
12Sending email from PHP scripts
- Sometimes it is useful to send email from a PHP
script - PHP uses mail() that by default sends e-mail via
the Simple Mail Transfer Protocol (SMTP). - mail(to_address, subject, message,
extra_headers)
Specify the Text of the email
Specify the destination email address.
Specify the subject line of the e-mail.
Specify additional email headers.
13Consider the following example
- 1. dest'orders_at_hardwareville.com'
- 2. subject 'New Hardware Order'
- 3. message 'Enclosed is a new order for 12
hammers.\n Thanks.' - 4. extra 'From harry_at_hardwareville.com'
- 5. mail( dest, subject, message, extra )
14Consider the following full example
- Implements save and notify
- Called from order2.php and saved at http//
- webwizard.aw.com/phppgm/C7/order3.php
- Can access variables product, quantity, and
sample_hidden sent as hidden fields from the
Billing Info form.
15The following PHP Script
- 1. lthtmlgtltheadgtlttitlegtOrder Product 3lt/titlegt
lt/headgtltbodygt - 2. lt?php
- 3. email'orders_at_hardwareville.com'
- 4. body "New Order Productproduct
Numberquantity Custname Codecode" - 5. print 'ltfont size4gt'
- 6. print "ltbrgtSending e-mail to order handling
department at email ... lt/fontgt" - 7. print "ltbrgtThe e-mail body is ltigt body.
lt/igt" - 8. from 'harry_at_hardwareville.com'
- 9. subject "New order from name"
- 10. mail(email, subject, body, "From from")
- 11. print 'ltbrgtltfont color"blue"gt E-mail sent.
Thanks for ordering. lt/fontgt' - 12. print "ltbrgtBy the way, sample
hiddensample_hidden" - 13. ?gtlt/bodygtlt/htmlgt
16The following PHP Script with REGISTER_GLOBALS
Off
- 1. lthtmlgtltheadgtlttitlegtOrder Product 3lt/titlegt
lt/headgtltbodygt - 2. lt?php
- 3. sample_hidden _POSTsample_hidden
quantity_POSTquantity - 4. product _POSTproduct
name_POSTname - 3. email'orders_at_hardwareville.com'
- 4. body "New Order Productproduct
Numberquantity Custname Codecode" - 5. print 'ltfont size4gt'
- 6. print "ltbrgtSending e-mail to order handling
department at email ... lt/fontgt" - 7. print "ltbrgtThe e-mail body is ltigt body.
lt/igt" - 8. from 'harry_at_hardwareville.com'
- 9. subject "New order from name"
- 10. mail(email, subject, body, "From from")
- 11. print 'ltbrgtltfont color"blue"gt E-mail sent.
Thanks for ordering. lt/fontgt' - 12. print "ltbrgtBy the way, sample
hiddensample_hidden" - 13. ?gtlt/bodygtlt/htmlgt
17Would have the following output
18Would have the following output
19Using Browser Cookies
- Cookies are small pieces of data that a Web
application can save when a user visits the Web
page. - Stored on the visitors hard drive
- a Web page script can read the previously stored
browser cookie data
20Understanding Cookie Limitations
- Users can easily disable the cookies feature.
- People move around.
- Users may delete cookies.
- PHP sets limit on cookies
21The disable cookie screen in Netscape
22Setting and Reading Cookies
- Cookies can be set in memory or on hard disk
- Set on memory are deleted when browser closes
- Those saved in HD retained until some defined
expiration date - Can use the setcookie() script
- setcookie('Customer_name', 'Denise')
Directs browser to create a cookie
Specify the cookies name
Specify the cookies value
23Setting A Cookie on a Hard Drive
- You need to use the time() function when want to
set a cookie on a hard drive.
24A full example of setting a cookie.
- Suppose a front-end web page asks for some survey
information - ltinput type"text" size"15" maxlength"20"
name"custname"gt - ltinput type"radio" name"prefers" value"power
tools" - checked gt Power Tools?
- ltinput type"radio" name"prefers"
- value"hand tools"gt Hand Tools?
- ltinput type"radio" name"prefers
- value"air fresheners"gt Air Fresheners?
25The following script runs when submitted
- 1. lt?php
- 2. expire time() (60 60 24 30)
- 3. setcookie("name", custname, expire)
- 4. setcookie("preference", prefers, expire)
- 5. ?gt
- 6. lthtmlgt
- 7. ltheadgtlttitlegtHappy Harry's Hardware Catalog
lt/titlegtlt/headgt - 8. ltbodygtltfont size4 color"blue"gt
- 9. lt?php
- 10. print "Thanks custname!
- 11. print Lets now look at prefers... "
- 12.?gt lt/fontgtlt/bodygtlt/htmlgt
26The following script runs when submitted (With
REGISTER_GLOBALS Off).
- 1. lt?php prefers _POSTprefers
expire_POSTexpire custname_POSTcustna
me - 2. expire time() (60 60 24 30)
- 3. setcookie("name", custname, expire)
- 4. setcookie("preference", prefers, expire)
- 5. ?gt
- 6. lthtmlgt
- 7. ltheadgtlttitlegtHappy Harry's Hardware Catalog
lt/titlegtlt/headgt - 8. ltbodygtltfont size4 color"blue"gt
- 9. lt?php
- 10. print "Thanks custname!
- 11. print Lets now look at prefers... "
- 12.?gt lt/fontgtlt/bodygtlt/htmlgt
27Would output
28Reading Cookies
- You can read a cookie by using a variable name
with the same name as a cookie - print cust_name
29Reading Cookies with REGISTER_GLOBALS Of
- To read a cookie value use the _COOKIE
associative array to get the cookie function - cust_name _COOKIEcust_name
30Example Script that read a cookie
- 1. lthtmlgt
- 2. ltheadgtlttitlegtHappy Harry's Hardware
Cataloglt/titlegt - 3. lt/headgtltbodygt
- 4. lt?php
- 5. print 'ltfont color"blue" size4gt'
- 6. if (isset(name))
- 7. print "Welcome back to our humble hardware
site, name." - 8. else
- 9. print 'ltfont color"red"gt'
- 10. print 'Welcome to our humble hardware
site.lt/fontgt' - 11.
- 12. if (preference 'hand tools')
- 13. print 'ltbrgt We have hammers on sale for 5
dollars!' - 14. elseif (preference 'power tools')
- 15. print 'ltbrgt We have power drills on sale
for 25 dollars!' - 16. elseif ( preference 'air
fresheners') - 17. print 'ltbrgt We now carry extra-strength
air fresheners!' - 18. else
- 19. print 'ltbrgt ltfont color"red"gt'
31Example Script that read a cookie
- 1. lthtmlgt
- 2. ltheadgtlttitlegtHappy Harry's Hardware
Cataloglt/titlegt - 3. lt/headgtltbodygt
- 4. lt?php name _COOKIEname preference
_COOKIEpreference - 5. print 'ltfont color"blue" size4gt'
- 6. if (isset(name))
- 7. print "Welcome back to our humble hardware
site, name." - 8. else
- 9. print 'ltfont color"red"gt'
- 10. print 'Welcome to our humble hardware
site.lt/fontgt' - 11.
- 12. if (preference 'hand tools')
- 13. print 'ltbrgt We have hammers on sale for 5
dollars!' - 14. elseif (preference 'power tools')
- 15. print 'ltbrgt We have power drills on sale
for 25 dollars!' - 16. elseif ( preference 'air
fresheners') - 17. print 'ltbrgt We now carry extra-strength
air fresheners!' - 18. else
- 19. print 'ltbrgt ltfont color"red"gt'
32PHP Sessions
- PHP supports two functions that enable you to
retain data between forms - session_start() - either starts a new session or
resumes one if a session exists - Run at the start of every script
- By default creates a unique session ID stored as
a cookie - session_register() - registers one or more
variables as session variables - name 'Matthew'
- preference 'Soccer Equipment'
- session_register('name', 'preference')
33Example PHP Code
- 1. lt?php session_start() ?gt
- 2. lthtmlgtltheadgtlttitlegtOrder Productlt/titlegt
- 3. lt/headgtltbodygt
- 4. ltform action
- "http//webwizard.aw.com/phppgm/C7/ses
sions2order.php" - method"post"gt
- 5. ltfont colorblue size5gt Hardware Product
Order Form lt/fontgt - 6. ltbrgt We have hammers, handsaws, and wrenches.
- 7. ltbrgtEnter Item ltinput text type"text"
size"15" maxlength"20" name"product"gt - 8. Enter Quantity ltinput text type"text"
size"15" maxlength"20" name"quantity"gtltbrgt - 9. lt?php
- 10. sample_hidden'Welcome Again!'
- 11. session_register('sample_hidden')
- 12. ?gt
- 13. ltbrgtltinput type"submit" value"Click To
Submit"gt - 14. ltinput type "reset" value "Reset" gt
- 15. lt/bodygtlt/htmlgt
34Example output
This script can be executed at
http//webwizard.aw.com/phppgm/C7/ordersession.
php
35Use the following script to read the session data
- 1. lt?php session_start() ?gt
- 2. lthtmlgtltheadgtlttitlegt Order Product 2 lt/titlegt
lt/headgt - 3. ltbodygt
- 4. ltform action"http//webwizard.aw.com/phppgm/C
7/sessions3order.php " method"post"gt - 5. lt?php
- 6. print "ltfont colorblue size4gt Sample
hidden sample_hiddenlt/fontgt" - 7. print "ltbrgtYou selected productproduct
and quantityquantity" - 8. session_register('product', 'quantity')
- 9. print 'ltbrgtPlease enter your name'
- 10. print 'ltinput text type"text" size"15"
maxlength"20" name"name"gt' - 11. print ' and Billing Code (5 digits)'
- 12. print 'ltinput text type"text" size"5"
maxlength"5" name"code"gt' - 13. print 'ltbrgt ltinput typesubmit
value"Process Order"gt' - 14. print 'ltinput typeresetgt'
- 15. print 'lt/formgtlt/bodygtlt/htmlgt'
- 16. ?gt
36Example output
This script can be executed at
http//webwizard.aw.com/phppgm/C7/ordersession.
php
37Some session extras
- session_is_registered() - can be used to
determine if a variable comes from a session - if (session_is_registered('name'))
- print "got namename from session"
- else
- print "namename not set from session"
-
38Session Extras - _SESSION
- Use _SESSION Associative array when
REGISTER_GLOBALS are off in php.ini - Do not need to use session_register()
- session_start()
- _SESSION'sample_hidden' 'Welcome!'
39Summary
- Hidden fields are HTML form fields you can use to
set a variable name and variable value without
displaying them on a form. - Cookies provide a way for Web server applications
to store small pieces of data on the users hard
disk. - PHP session functions provide a convenient way to
retain data between PHP scripts. - Use session_start() and session_register()
functions to start sessions and define session
variables, respectively