Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizards Guide to PHP by David Lash.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 8 Managing End-User Sessions.
Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizard’s Guide to PHP by David Lash.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 8 Managing End-User Sessions.
XP Tutorial 9 New Perspectives on JavaScript, Comprehensive1 Working with Cookies Managing Data in a Web Site Using JavaScript Cookies.
Chapter 10 Managing State Information PHP Programming with MySQL.
CSE 154 LECTURE 13: SESSIONS. Expiration / persistent cookies setcookie("name", "value", expiration); PHP $expireTime = time() + 60*60*24*7; # 1 week.
Chapter 10 Maintaining State Information Using Cookies.
1 PHP and MySQL David Lash Module 2 Working with forms, PHP conditionals and loops.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
1 Creating Web Forms in HTML Web forms collect information from customers Web forms include different control elements including: –Input boxes –Selection.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Comp2513 Forms and CGI Server Applications Daniel L. Silver, Ph.D.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Working with Cookies Managing Data in a Web Site Using JavaScript Cookies* *Check and comply with the current legislation regarding handling cookies.
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Slide 7-1 CHAPTER 7 Managing Multiple-Form Applications: Writing scripts with multiple screens.
HTML Forms.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Week 9 - Form Basics Key Concepts 1. 1.Describe common uses of forms on web pages 2.Create forms on web pages using the form, input, textarea, and select.
Cookies & Session Web Technology
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
PHP Programming with MySQL Slide 10-1 CHAPTER 10 Managing State Information.
Topics Sending an Multipart message Storing images Getting confirmation Session tracking using PHP Graphics Input Validators Cookies.
PHP. $_GET / $_POST / $_SESSION PHP uses predefined variables to provide access to important information about the server and requests from a browser.
COOKIES and SESSIONS. COOKIES A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
Sessions in PHP – Page 1 of 13CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Sessions in PHP Reading: Williams.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
Sessions and Cookies State Management, Cookies, Sessions, Hidden Fields SoftUni Team Technical Trainers Software University
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
Form Processing Week Four. Form Processing Concepts The principal tool used to process Web forms stored on UNIX servers is a CGI (Common Gateway Interface)
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
How to maintain state in a stateless web Shirley Cohen
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
 A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
Cookies and Sessions in PHP. Arguments for the setcookie() Function There are several arguments you can use i.e. setcookie(‘name’, ‘value’, expiration,
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
ITM 352 Cookies.
Web Programming Language
Cookies and Sessions in PHP
Open Source Programming
Cookies BIS1523 – Lecture 23.
Web Development & Design Foundations with H T M L 5
Creating Forms on a Web Page
Web Programming Language
PHP-II.
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
Presentation transcript:

Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash

Copyright © 2003 Pearson Education, Inc. Slide 7-2 CHAPTER 7 Managing Multiple-Form Applications: Writing scripts with multiple screens

Copyright © 2003 Pearson Education, Inc. 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

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.

Copyright © 2003 Pearson Education, Inc. Slide 7-7 A Full Script Example Consider an example script sets a hidden field Implements the Order Info form on submit sends data to php

Copyright © 2003 Pearson Education, Inc. Slide 7-8 PHP Script 1. Order Product 2. <form action=" method="post"> 3. Happy Harry's Hardware Product Order Form We have hammers, handsaws, and wrenches on special today! Enter Item: 9. Enter Quantity:

Copyright © 2003 Pearson Education, Inc. Slide 7-9 The Output...

Copyright © 2003 Pearson Education, Inc. Receiving Hidden Fields in Web Sessions Your scripts can receive data from hidden fields like any other data. Suppose the following is stored at:

Copyright © 2003 Pearson Education, Inc. Slide 7-11 Receiving PHP Script 1. Order Product <?php $sample_hidden = $_POST[“sample_hidden”]; 5. $product = $_POST[“product”]; $quantity = $_POST[“quantity”]; 5. print " "; 6. print "Hidden value=$sample_hidden "; 7. print "You selected product=$product and quantity=$quantity"; 8. print " "; 9. print " "; 10. print " "; 11. print 'Please enter your name:'; 12. print ' '; 13. print ' and billing code: (5 digits)'; 14. print '<input type="text" size="5" maxlength="5" name="code">'; 15. print ' '; 16. print ' '; 17. ?>

Copyright © 2003 Pearson Education, Inc. Slide 7-12 Sending from PHP scripts Sometimes it is useful to send from a PHP script: PHP uses mail() that by default sends via the Simple Mail Transfer Protocol (SMTP). mail(to_address, subject, message, extra_headers); Specify the destination address. Specify the subject line of the . Specify the Text of the Specify additional headers.

Copyright © 2003 Pearson Education, Inc. Slide 7-13 Consider the following example … $subject = 'New Hardware Order'; 3. $message = 'Enclosed is a new order for 12 hammers.\n Thanks.'; 4. $extra = 'From: 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 webwizard.aw.com/~phppgm/C7/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 Script 1. Order Product 3 2. <?php 3. $sample_hidden = $_POST[“sample_hidden”]; quantity=$_POST[“$quantity”]; 4. $product = $_POST[“product”]; $name=$_POST[“name”]; $body = "New Order: Product=$product Number=$quantity Cust=$name Code=$code"; 5. print ' '; 6. print " Sending to order handling department at $ ... "; 7. print " The body is : $body. "; 8. $from = 9. $subject = "New order from $name"; 10. mail($ , $subject, $body, "From: $from"); 11. print ' sent. Thanks for ordering. '; 12. print " By the way, sample hidden=$sample_hidden"; 13. ?>

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 The disable cookie screen in Netscape

Copyright © 2003 Pearson Education, Inc. Slide 7-21 Setting and Reading Cookies Cookies can be set in memory or on hard disk Set on memory are deleted when browser closes Can use the setcookie() script 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-22 Setting A Cookie on a Hard Drive You need to use the time() function when want to set a cookie on a hard drive.

Copyright © 2003 Pearson Education, Inc. Slide 7-23 A full example of setting a cookie…. Suppose a front-end web page asks for some survey information: <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-24 The following script runs when submitted: 1.<?php $prefers = $_POST[“prefers”]; $expire=$_POST[“expire”]; $custname=$_POST[“custname”]; 2. $expire = time() + (60 * 60 * 24 * 30); 3. setcookie("name", $custname, $expire); 4. setcookie("preference", $prefers, $expire); 5.?> Happy Harry's Hardware Catalog 8. 9.<?php 10. print "Thanks $custname! “; 11. print “Let’s now look at $prefers... "; 12.?>

Copyright © 2003 Pearson Education, Inc. Slide 7-25 Would output:

Copyright © 2003 Pearson Education, Inc. Slide 7-26 Reading Cookies To read a cookie value use the $_COOKIE[] associative array to get the cookie function $cust_name= $_COOKIE[“cust_name”];

Copyright © 2003 Pearson Education, Inc. Slide 7-27 Example Script that read a cookie Happy Harry's Hardware Catalog 3. 4.<?php $name = $_COOKIE[“name”]; $preference = $_COOKIE[“preference”]; 5. print ' '; 6. if (isset($name)){ 7. print "Welcome back to our humble hardware site, $name."; 8. } else { 9. print ' '; 10. print 'Welcome to our humble hardware site. '; 11. } 12. if ($preference == 'hand tools'){ 13. print ' We have hammers on sale for 5 dollars!'; 14. } elseif ($preference == 'power tools'){ 15. print ' We have power drills on sale for 25 dollars!'; 16. } elseif ( $preference == 'air fresheners'){ 17. print ' We now carry extra-strength air fresheners!'; 18. } else { 19. print ' '; 20. print 'We have drills and hammers on special today!'; 21. } 22. ?>

Copyright © 2003 Pearson Education, Inc. Slide 7-28 PHP 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 To store a value in a session variable, simply assign it to the $_SESSION associative array $name = 'Matthew'; $preference = 'Soccer Equipment'; $_SESSION[‘name’] = $name; $_SESSION[‘preference’] = $preference;

Copyright © 2003 Pearson Education, Inc. Slide 7-29 Example PHP Code Order Product 3. 4.<form action= " method="post"> 5. Hardware Product Order Form 6. We have hammers, handsaws, and wrenches. 7. Enter Item: 8.Enter Quantity: 9.<?php 10. $sample_hidden='Welcome Again!'; 11. $_SESSION[‘sample_hidden’] = $sample_hidden; 12.?>

Copyright © 2003 Pearson Education, Inc. Slide 7-30 Example output

Copyright © 2003 Pearson Education, Inc. Slide 7-31 Use the following script to read the session data Order Product <?php 6. print " Sample hidden= $sample_hidden "; 7. print " You selected product=$product and quantity=$quantity"; 8.$product = $_SESSION[‘product’]; 9.$quantity = $_SESSION[‘quantity’]; 10. print ' Please enter your name'; 11. print ' '; 12. print ' and Billing Code: (5 digits)'; 13. print ' '; 14. print ' '; 15. print ' '; 16. print ' '; 17.?>

Copyright © 2003 Pearson Education, Inc. Slide 7-32 Example output

Copyright © 2003 Pearson Education, Inc. Slide 7-33 Some session extras isset() can be used to determine if a variable comes from a session: if (isset($_SESSION['name‘])){ print "got name=$name from session"; } else { print "name=$name not set from session"; }

Copyright © 2003 Pearson Education, Inc. Slide 7-34 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 session_start() function and $_SESSION associative array to start sessions and define session variables, respectively