1 / 31

DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida

Media Software Design. DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida. The Objective: Learn how PHP code can remember data as a session continues, via "hidden variables" "cookies" "session variables". First, a definition: Session. Wikipedia.org.

moeshe
Télécharger la présentation

DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Media Software Design DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida

  2. The Objective: • Learn how PHP code can remember • data as a session continues, via • "hidden variables" • "cookies" • "session variables"

  3. First, a definition: Session Wikipedia.org "… a semi-permanent interactive information interchange, also known as a dialogue, a conversation or a meeting" (wikipedia)* A web session continues while (a) you have not closed the browser, and (b) you continue to interact with pages from the same website. Most browsers can support several concurrent sessions, via tabs or multiple pages. 3 3 3 3 3

  4. Sessions … Wikipedia.org … often have timing constraints – They may "time out" if there is no activity for a period of time. … can be implemented in several ways. We discuss three techniques today. 4 4 4 4 4 4

  5. What is the problem? An Example The Guessing Game: Browser Server Run guess.php Guess.php This is a guessing game. (Target=17) Enter your guess: Try Quit

  6. Behind the Scenes: The Guessing Game: (First try – (won't work!)) // guess.php Browser Server This is a guessing game. (Target=17) Enter your guess: Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'>"; } else if ($act=='Try') { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … Try Quit <html> …<form …> This … Target=17.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> 6

  7. Behind the Scenes: Another way to handle the 'act': // guess.php Browser Server This is a guessing game. (Target=17) Enter your guess: Print "<html> …<form …>"; $act=$_POST['act']; If (!isset($_POST['act'])) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'>"; } else if ($act=='Try') { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … Try Quit <html> …<form …> This … Target=17.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> 7 7

  8. Enter 4, click "Try" // guess.php Browser Server This is a guessing game. (Target=17) Enter your guess: Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'>"; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … 4 Try Quit <html> …<form …> This … Target=17.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> 8 8

  9. Enter 4, click "Try" Browser // guess.php Server request guess.php with Post data: act->'Try' guessvalue->4 Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'>"; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … What happens in server? $act $guess $target 9 9

  10. Enter 4, click "Try" Browser // guess.php Server request guess.php with Post data: act->'Try' guessvalue->4 Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … What happens in server? $act Try $guess $target 10 10 10

  11. Enter 4, click "Try" Browser // guess.php Server request guess.php with Post data: act->'Try' guessvalue->4 Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'>"; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … What happens in server? $act Try $guess 4 $target 11 11 11

  12. Enter 4, click "Try" Browser // guess.php Server request guess.php with Post data: act->'Try' guessvalue->4 Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'>"; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … $act Try $guess 4 $target What happens now? 12 12 12 12

  13. Enter 4, click "Try" Browser // guess.php Server request guess.php with Post data: act->'Try' guessvalue->4 Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'>"; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … $act Try $guess 4 $target What happens now? Server doesn't remember $target! 13 13 13 13 13

  14. Behind the Scenes: The Guessing Game, with a Hidden Variable // guessh.php Browser Server Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> <input type='hidden' name='tarvalue' value=$target>"; } else if ($act=='Try') { $guess=$_POST['guessvalue']; $target=$_POST['tarvalue']; if ($guess<$target) print ("Too low!"); else … This is a guessing game. (Target=17) Enter your guess: Try Quit <html> …<form …> This … Target=17.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> <input type='hidden' name='tarvalue' Value=17> 14 14

  15. Behind the Scenes: The Guessing Game, with a Hidden Variable // guessh.php Browser Server Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> <input type='hidden' name='tarvalue' value=$target>"; } else if ($act=='Try') { $guess=$_POST['guessvalue']; $target=$_POST['tarvalue']; if ($guess<$target) print ("Too low!"); else … This is a guessing game. (Target=17) Enter your guess: 4 Try Quit <html> …<form …> This … Target=17.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> <input type='hidden' name='tarvalue' Value=17> 15 15 15

  16. Behind the Scenes:Try 2 The Guessing Game, with a Hidden Variable Browser // guessh.php Browser Server request guessh.php with Post data: act->'Try' guessvalue->4 tarvalue ->17 Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> <input type='hidden' name='tarval' value=$target>"; } else if ($act=='Try') { $guess=$_POST['guessvalue']; $target=$_POST['tarvalue']; if ($guess<$target) print ("Too low!"); else … $act $guess $target What happens now? 16 16 16

  17. Behind the Scenes:Try 2 The Guessing Game, with a Hidden Variable Browser // guessh.php Browser Server request guessh.php with Post data: act->'Try' guessvalue->4 tarvalue ->17 Print "<html> …<form …>"; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target.<br /> Enter your guess:<br /> <input name='guessvalue'> <input type='submit' name='act' value='Try'> <input type='submit' name='act' value='Quit'> <input type='hidden' name='tarval' value=$target>"; } else if ($act=='Try') { $guess=$_POST['guessvalue']; $target=$_POST['tarvalue']; if ($guess<$target) print ("Too low!"); else … $act Try $guess 4 $target 17 What happens now? Program works, because the Browser CARRIED FORWARD The value of $target, in 'tarvalue'! 17 17 17 17

  18. Assignment #1 for next Tuesday: Use the code fragments on the previous pages to make a "guessing game" that works with a HIDDEN VARIABLE to remember the target value. Remove the code that prints "Target=$target" so it's a real guessing game. Use 'View Source' in your browser to see the target value. Your group's code MAY be demonstrated! 18 18

  19. If Hidden Variables are so cool.. Why not use 'em everywhere? • You have to hand-craft for every occasion. • You expose all your state information to the • 'view source' function in the browser. • Once you close the browser, all is lost. • (What if I wanted to remember for a WEEK?) • SO, a more sophisticated way of having the Browser • remember things, had to be invented. • Can you guess what it might be called? • There is a hint on this slide …. 19

  20. Cookies! • Software puts cookies onto your browser • Original purpose: to track return visits to a site. • What's a cookie: • a url (from which it was sent) • a name (like "id") • a value (like 34%FA-JM334-NPQXf42) • that is unique to each visitor. • It might ENCODE your information, or just KEY back into their database about you! • * A path – what part of the installing site can use this cookie. • (Go look at my cookies.) 20 20

  21. A Cookie Example Desired Behavior: Browser Server Welcome back, Hortense! How can we help? If customer has Been here before (within a week), Welcome them back. (1) Otherwise, Get their name. (2) Then do business.(3) 1 welcome.php Buy Stuff Return Stuff Welcome to our store! Please enter your name. 2 3 How can we help? Continue Buy Stuff Return Stuff 21 21 21

  22. A Cookie Example <?php $namec=$_COOKIE['namecookie']; if ($namec) makeform1 ($namec); else { $namev=$_POST['namevalue']; if (!$namev) makeform2( ); // ask for name else { $untiltime=time()+60*60*24*7; setcookie('namecookie', $namev, $untiltime); makeform3($namev); // } }?> Welcome back, Hortense! How can we help? 1 Buy Stuff Return Stuff 2 Welcome to our store! Please enter your name. 3 How can we help? Continue Buy Stuff Return Stuff 22 22 22

  23. Cookies! Assignment #2 for Tue Use the <hidden> version of the guessing Game (that you built) as a starting point. Use the example cookie code As an example of how to work with cookies. It is in the file 'examples.cookie.txt' Build a guessing game that uses a cookie Named 'targetc' rather than the hidden field 'tarvalue' to store and retrieve the guess-target Number. 23 23 23

  24. Cookies … and then what? Session Variables! Storing small information in cookies is OK But there's a better way. Use the cookies to store an IDENTIFIER And store LOTS of data on the server, instead! The cookie is automatically created and managed By PHP's session system. Here's how it works. 24 24 24 24

  25. Session Variables The very first thing that must happen In your code: <?php session_start(); ?> To store data for future use: $_SESSION['itemname']=$item; To retrieve data: $item=$_SESSION['itemname']; You can use any label name and any variable name I like to keep them consistent

  26. Session Variables • What's the difference? • Session variables are stored in SERVER • while cookies are stored in BROWSER • Cookies are very small (just single variables) • but session variables can hold huge • arrays, texts, etc. without a problem. • Look at the code for welcomesess.php 26

  27. Session Variables Three general rules to avoid trouble: 1) bring out all your session variables at the head of the main part of your code. $lowval=$_SESSION['lowval']; 2) Put away all your session variables at the end of main: $_SESSION['lowval'] = $lowval; 3) Don't touch $_SESSION in between! 27

  28. ++ Session vars! Assignment #3 for Tue Use the cookie version of the guessing Game (that you built) as a starting point. Use the example code ’examples.session.txt' As an example of how to work with session vars. Build a guessing game that uses a session variable Named 'targetsess' rather than the cookie 'targetc' to store and retrieve the guess-target Number. 28 28 28 28

  29. Experiments with Session Variables Etsy.com. Remember: The cookies are like 'coat-check receipts'. The server gives them to the user's browser to be used to reclaim the user's session data. Look at, and play with, the example 'worry.php'. And also its helper, 'worryclear.php'. (They’re in the examples.session.txt file.) 29 29

  30. The function 'isset' Etsy.com. I don't use it much, but some folks do. It tells you if a variable has been assigned a value. It returns 'true' even if the value is 0 or (empty). 30 30 30

  31. And next week .. Havban.wordpress.com We will begin to focus on DEBUGGING skills and techniques – the >>>>most important content of this course <<<< 31 31 31

More Related