DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida
description
Transcript of DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida
1
DIG 3134
Lecture 6: Maintaining State
Michael MoshellUniversity of Central Florida
Media Software Design
2
The Objective:• Learn how PHP code can remember
data as a session continues, via-"hidden variables"- "cookies"- "session variables"
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 notclosed the browser, and (b) you continue to interactwith pages from the same website.
Most browsers can support several concurrent sessions, via tabs or multiple pages.
Wikipedia.org
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
What is the problem? An ExampleThe Guessing Game:
This is a guessing game. (Target=17)Enter your guess:
Browser Server
Guess.phpRun guess.php
Try Quit
66
Behind the Scenes:The Guessing Game: (First try – (won't work!))
This is a guessing game. (Target=17)Enter your guess:
Browser Server // guess.php
Try Quit
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 …
<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'>
777
Behind the Scenes:Another way to handle the 'act':
This is a guessing game. (Target=17)Enter your guess:
Browser Server // guess.php
Try Quit
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 …
<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'>
888
This is a guessing game. (Target=17)Enter your guess:4
Browser Server // guess.php
Try Quit
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 …
<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'>
Enter 4, click "Try"
999
Enter 4, click "Try"
BrowserServer // guess.php
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 …
request guess.php with Post data: act->'Try' guessvalue->4
What happens in server?
$act
$guess
$target
10101010
Enter 4, click "Try"
BrowserServer // guess.php
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 …
request guess.php with Post data: act->'Try' guessvalue->4
What happens in server?
Try$act
$guess
$target
11111111
Enter 4, click "Try"
BrowserServer // guess.php
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 …
request guess.php with Post data: act->'Try' guessvalue->4
What happens in server?
$act
$guess
$target
Try
4
1212121212
Enter 4, click "Try"
BrowserServer // guess.php
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 …
request guess.php with Post data: act->'Try' guessvalue->4
What happens now?
$act
$guess
$target
Try
4
131313131313
Enter 4, click "Try"
BrowserServer // guess.php
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 …
request guess.php with Post data: act->'Try' guessvalue->4
What happens now?Server doesn't remember $target!
$act
$guess
$target
Try
4
141414
Behind the Scenes:The Guessing Game, with a Hidden Variable
This is a guessing game. (Target=17)Enter your guess:
Browser Server// guessh.php
Try Quit
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 …
<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>
15151515
Behind the Scenes:The Guessing Game, with a Hidden Variable
This is a guessing game. (Target=17)Enter your guess:
Browser Server// guessh.php
Quit
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 …
<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>
4 Try
16161616
Behind the Scenes:Try 2The Guessing Game, with a Hidden Variable
Browser Server// guessh.php
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 …
Browser
request guessh.php with Post data: act->'Try' guessvalue->4 tarvalue ->17
What happens now?
$act
$guess
$target
1717171717
Behind the Scenes:Try 2The Guessing Game, with a Hidden Variable
Browser Server// guessh.php
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 …
Browser
request guessh.php with Post data: act->'Try' guessvalue->4 tarvalue ->17
What happens now?Program works, because theBrowser CARRIED FORWARDThe value of $target, in 'tarvalue'!
$act
$guess
$target
Try
4
17
181818
Assignment #1 for next Tuesday:
Use the code fragments on the previous pagesto make a "guessing game" that workswith a HIDDEN VARIABLE to rememberthe target value.
Remove the code that prints "Target=$target"so it's a real guessing game.
Use 'View Source' in your browser to see thetarget value.
Your group's code MAY be demonstrated!
1919
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 Browserremember things, had to be invented.
Can you guess what it might be called?There is a hint on this slide ….
202020
Cookies!
1) Software puts cookies onto your browser2) 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.)
21212121
A Cookie Example
Welcome to our store!
Please enter your name.
Browser Server
welcome.php
Continue
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
22222222
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.
Continue
Welcome back,Hortense! How can
we help?Buy Stuff Return Stuff
1
2
How can we help?Buy Stuff Return Stuff
3
23232323
Cookies! Assignment #2 for Tue
Use the <hidden> version of the guessingGame (that you built) as a starting point.
Use the example cookie codeAs an example of how to work with cookies.
It is in the file 'examples.cookie.txt'
Build a guessing game that uses a cookieNamed 'targetc' rather than the hidden field'tarvalue' to store and retrieve the guess-targetNumber.
2424242424
Cookies … and then what?Session Variables!
Storing small information in cookies is OKBut there's a better way.
Use the cookies to store an IDENTIFIERAnd store LOTS of data on the server, instead!
The cookie is automatically created and managedBy PHP's session system. Here's how it works.
25
The very first thing that must happenIn 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
2626
Session Variables What's the difference?
•Session variables are stored in SERVERwhile cookies are stored in BROWSER
•Cookies are very small (just single variables)but session variables can hold hugearrays, texts, etc. without a problem.
Look at the code for welcomesess.php
2727
Session Variables
Three general rules to avoid trouble:
1) bring out all your session variables at thehead of the main part of your code.
$lowval=$_SESSION['lowval'];
2) Put away all your session variables at theend of main:
$_SESSION['lowval'] = $lowval;
3) Don't touch $_SESSION in between!
2828282828
Session vars! Assignment #3 for Tue
Use the cookie version of the guessingGame (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 variableNamed 'targetsess' rather than the cookie 'targetc' to store and retrieve the guess-targetNumber.
++
292929
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.
30303030
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.
31313131
And next week ..
We will begin to focus on DEBUGGING skills and techniques – the
>>>> most important contentof this course <<<<
Havban.wordpress.com