Download presentation
Presentation is loading. Please wait.
Published byVirgil Maxwell Modified over 8 years ago
1
1 DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida Media Software Design
2
2 The Objective: Learn how PHP code can remember data as a session continues, via -"hidden variables" - "cookies" - "session variables"
3
333333 First, a definition: Session "… 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. Wikipedia.org
4
4444444 Sessions … … 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. Wikipedia.org
5
5 What is the problem? An Example The Guessing Game: This is a guessing game. (Target=17) Enter your guess: This is a guessing game. (Target=17) Enter your guess: BrowserServer Guess.php Run guess.php Try Quit
6
66 Behind the Scenes: The Guessing Game: (First try – (won't work!)) This is a guessing game. (Target=17) Enter your guess: This is a guessing game. (Target=17) Enter your guess: BrowserServer // guess.php Try Quit Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: "; } else if ($act=='Try') { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … … This … Target=17. Enter your guess:
7
777 Behind the Scenes: Another way to handle the 'act': This is a guessing game. (Target=17) Enter your guess: This is a guessing game. (Target=17) Enter your guess: BrowserServer // guess.php Try Quit Print " … "; $act=$_POST['act']; If (!isset($_POST['act'])) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: "; } else if ($act=='Try') { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … … This … Target=17. Enter your guess:
8
888 This is a guessing game. (Target=17) Enter your guess: This is a guessing game. (Target=17) Enter your guess: 4 4 BrowserServer // guess.php Try Quit Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: "; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … … This … Target=17. Enter your guess: Enter 4, click "Try"
9
999 Browser Server // guess.php Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: "; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … request guess.php with Post data: act->'Try' guessvalue->4 What happens in server? $act $guess $target
10
10 Enter 4, click "Try" Browser Server // guess.php Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … request guess.php with Post data: act->'Try' guessvalue->4 What happens in server? Try $act $guess $target
11
11 Enter 4, click "Try" Browser Server // guess.php Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: "; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … request guess.php with Post data: act->'Try' guessvalue->4 What happens in server? $act $guess $target Try 4 4
12
12 Enter 4, click "Try" Browser Server // guess.php Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: "; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … request guess.php with Post data: act->'Try' guessvalue->4 What happens now? $act $guess $target Try 4 4
13
13 Enter 4, click "Try" Browser Server // guess.php Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: "; } else { $guess=$_POST['guessvalue']; if ($guess<$target) print ("Too low!"); else … request guess.php with Post data: act->'Try' guessvalue->4 What happens now? Server doesn't remember $target! $act $guess $target Try 4 4
14
14 Behind the Scenes: The Guessing Game, with a Hidden Variable This is a guessing game. (Target=17) Enter your guess: This is a guessing game. (Target=17) Enter your guess: BrowserServer // guessh.php Try Quit Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: <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 … Target=17. Enter your guess: <input type='hidden' name='tarvalue' Value=17>
15
15 Behind the Scenes: The Guessing Game, with a Hidden Variable This is a guessing game. (Target=17) Enter your guess: This is a guessing game. (Target=17) Enter your guess: BrowserServer // guessh.php Quit Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: <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 … Target=17. Enter your guess: <input type='hidden' name='tarvalue' Value=17> 4 4 Try
16
16 Behind the Scenes:Try 2 The Guessing Game, with a Hidden Variable BrowserServer // guessh.php Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: <input type='hidden' name='tarval' value=$target>"; } else if ($act=='Try') { $guess=$_POST['guessvalue']; $target=$_POST['tarvalue']; if ($guess<$target) print ("Too low!"); else … Browser request guessh.php with Post data: act->'Try' guessvalue->4 tarvalue ->17 What happens now? $act $guess $target
17
17 Behind the Scenes:Try 2 The Guessing Game, with a Hidden Variable BrowserServer // guessh.php Print " … "; $act=$_POST['act']; If (!$act) // no previous form { $target=rand(1,100); print "This … Target=$target. Enter your guess: <input type='hidden' name='tarval' value=$target>"; } else if ($act=='Try') { $guess=$_POST['guessvalue']; $target=$_POST['tarvalue']; if ($guess<$target) print ("Too low!"); else … Browser request guessh.php with Post data: act->'Try' guessvalue->4 tarvalue ->17 What happens now? Program works, because the Browser CARRIED FORWARD The value of $target, in 'tarvalue'! $act $guess $target Try 4 4 17
18
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!
19
19 If Hidden Variables are so cool.. Why not use 'em everywhere? 1)You have to hand-craft for every occasion. 2)You expose all your state information to the 'view source' function in the browser. 3)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 ….
20
20 Cookies! 1)Software puts cookies onto your browser 2)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.)
21
21 A Cookie Example Welcome to our store! Please enter your name. Welcome to our store! Please enter your name. Browser Server welcome.php Continue Welcome back, Hortense! How can we help? Welcome back, Hortense! How can we help? Buy Stuff Return Stuff 1 2 Desired Behavior: If customer has Been here before (within a week), Welcome them back. (1) Otherwise, Get their name. (2) Then do business.(3) How can we help? 3 Buy Stuff Return Stuff
22
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 to our store! Please enter your name. Welcome to our store! Please enter your name. Continue Welcome back, Hortense! How can we help? Welcome back, Hortense! How can we help? Buy Stuff Return Stuff 1 2 How can we help? Buy Stuff Return Stuff 3
23
23 Cookies! Assignment #2 for Tue Use the 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.
24
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.
25
25 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']; Session Variables You can use any label name and any variable name I like to keep them consistent
26
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
27
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!
28
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. ++
29
29 Experiments with Session Variables 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.) Etsy.com.
30
30 The function 'isset' 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). Etsy.com.
31
31 And next week.. We will begin to focus on DEBUGGING skills and techniques – the >>>> most important content of this course <<<< Havban.wordpress.com
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.