copyright © 2003 pearson education, inc. slide 7-1 the web wizard’s guide to php by david lash
TRANSCRIPT
Copyright © 2003 Pearson Education, Inc. Slide 7-1
The Web Wizard’s Guide to PHPby David Lash
Copyright © 2003 Pearson Education, Inc. Slide 7-2
CHAPTER 7Managing Multiple-Form
Applications:Writing scripts with multiple screens
Copyright © 2003 Pearson Education, Inc. Slide 7-3
Objectives 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
Copyright © 2003 Pearson Education, Inc. Slide 7-4
What 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.
Copyright © 2003 Pearson Education, Inc. Slide 7-5
Example Multiple Screen Session
Order Info
Present formasking forproduct andquantity.
Billing Info
Present formrequesting name and billingcode.
If mistakeremember allinformationentered & ask again.
Save andNotifySave to a fileor databaseand sendemail.
If mistakeask again
Copyright © 2003 Pearson Education, Inc. Slide 7-6
Using 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 user’s who view source.
<input type=”hidden” name=”preference” value=”Likes Power Tools”>
Variable namethat will be availableto your PHP program
Variable's value in thePHP program
Copyright © 2003 Pearson Education, Inc. Slide 7-7
A Full Script Example
Consider an example script that sets a hidden field Implements the Order Info form On submit sends data to order2.php
Copyright © 2003 Pearson Education, Inc. Slide 7-8
PHP Script
1. <html><head><title>Order Product</title></head><body>2. <form action="http://webwizard.aw.com/~phppgm/C7/order2.php"method="post">3. <font color="blue" size="5"> Happy Harry's Hardware ProductOrder Form</font>4. <br><font color="red" size="4">5. We have hammers, handsaws, and wrenches on special today!6. </font>7. <input type="hidden" name="sample_hidden" value="Welcome!">8. <br>Enter Item: <input text type="text" size="15” maxlength="20" name="product">9. Enter Quantity: <input text type="text" size="15” maxlength="20"
name="quantity"><br>10. <br><input type="submit" value="Click To Submit">11. <input type = "reset" value="Reset">12. </form></body></html>
Copyright © 2003 Pearson Education, Inc. Slide 7-9
The Output ...The previous code can be executed at
http://itm325.itmbsu.net/jstudent/Examples/Ch7/order.html
Copyright © 2003 Pearson Education, Inc. Slide 7-10
Receiving Hidden Fields in Web Sessions
Your scripts can receive data from hidden fields like any other data. Suppose the following is stored at order2.php
Copyright © 2003 Pearson Education, Inc. Slide 7-11
Receiving PHP
Show Source Code
Copyright © 2003 Pearson Education, Inc. Slide 7-12
Sending 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 destination email address.
Specify the subject line of the e-mail.
Specify the Text of the email
Specify additional email headers.
Copyright © 2003 Pearson Education, Inc. Slide 7-13
Consider the following example …1. $dest='[email protected]';
2. $subject = 'New Hardware Order';
3. $message = 'Enclosed is a new order for 12 hammers.\n Thanks.';
4. $extra = 'From: [email protected]';
5. mail( $dest, $subject, $message, $extra );
Copyright © 2003 Pearson Education, Inc. Slide 7-14
Consider the following full example …
Implements save and notify Called from order2.php and saved at order3.php Can access variables $product, $quantity,
and $sample_hidden sent as hidden fields from the Billing Info form.
Copyright © 2003 Pearson Education, Inc. Slide 7-15
The following PHP ScriptShow Source Code
Copyright © 2003 Pearson Education, Inc. Slide 7-16
Would have the following output …
Copyright © 2003 Pearson Education, Inc. Slide 7-17
Would have the following output …
Copyright © 2003 Pearson Education, Inc. Slide 7-18
Using Browser Cookies … Cookies are small pieces of data that a Web
application can save when a user visits the Web page. Stored on the visitor’s hard drive A web page script can read the previously
stored browser cookie data
Copyright © 2003 Pearson Education, Inc. Slide 7-19
Understanding Cookie Limitations
Users can easily disable the cookies feature.
People move around. Users may delete cookies. PHP sets limit on cookies
Copyright © 2003 Pearson Education, Inc. Slide 7-20
Setting and Reading Cookies
Cookies can be set in memory or on hard disk Set in memory are deleted when browser closes Can use the setcookie() function
setcookie('Customer_name', 'Denise');
Directs browser to create a cookie
Specify the cookie’s name
Specify the cookie’s value
Copyright © 2003 Pearson Education, Inc. Slide 7-21
Setting A Cookie on a Hard Drive
You need to use the time() function when you want to set a cookie on a hard drive.
$expire = time() + (60 * 60 * 24 * 10);setcookie('Preference', 'Likes Chocolate', $expire);
Returns the currentseconds since 1/1/70
Cookie name
Seconds/minute
Minutes/hour
Hours/dayNumber of days
Cookie value Cookie expiration time.
Copyright © 2003 Pearson Education, Inc. Slide 7-22
A full example of setting a cookie….
Suppose a front-end web page asks for some survey information:
<input type="text" size="15" maxlength="20" name="custname"><input type="radio" name="prefers" value="power tools" checked > Power Tools?<input type="radio" name="prefers"
value="hand tools"> Hand Tools?
<input type="radio" name="prefers”
value="air fresheners"> Air Fresheners?
Copyright © 2003 Pearson Education, Inc. Slide 7-23
The following script runs when submitted:
Show Source Code
Copyright © 2003 Pearson Education, Inc. Slide 7-24
Would output:The previous code can be executed at: http://itm325.itmbsu.net/jstudent/Examples/Ch7/setcookie1.html
Copyright © 2003 Pearson Education, Inc. Slide 7-25
Reading Cookies To read a cookie value use the $_COOKIE[ ]
associative array to get a cookie value $cust_name= $_COOKIE[“cust_name”];
Copyright © 2003 Pearson Education, Inc. Slide 7-26
Example Script that read a cookie
Show Source Code
Execute code: http://itm325.itmbsu.net/jstudent/Examples/Ch7/readcookie.php
Copyright © 2003 Pearson Education, Inc. Slide 7-27
PHP Sessions
PHP supports sessions 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 variables are accessed through the
$_SESSION associative array.
Copyright © 2003 Pearson Education, Inc. Slide 7-28
Example PHP Code
Source Code
Copyright © 2003 Pearson Education, Inc. Slide 7-29
Use the following script to read the session data
Source Code
Copyright © 2003 Pearson Education, Inc. Slide 7-30
Example outputThis script can be executed at: http://itm325.itmbsu.net/jstudent/Examples/Ch7/ordersession.php
Copyright © 2003 Pearson Education, Inc. Slide 7-31
Summary
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 user’s hard disk.
PHP session functions provide a convenient way to retain data between PHP scripts. Use the session_start() function to start sessions