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

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

Jan 18, 2018

Download

Documents

Virgil Maxwell

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida Media Software Design.

1

DIG 3134

Lecture 6: Maintaining State

Michael MoshellUniversity of Central Florida

Media Software Design

Page 2: 1 DIG 3134 Lecture 6: Maintaining State Michael Moshell University 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"

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

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

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

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

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

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

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

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'>

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

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'>

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

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"

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

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

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

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

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

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

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

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

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

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

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

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>

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

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

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

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

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

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

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

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!

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

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 ….

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

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.)

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

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

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

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

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

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.

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

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.

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

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

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

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

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

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!

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

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.

++

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

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.

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

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.

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

31313131

And next week ..

We will begin to focus on DEBUGGING skills and techniques – the

>>>> most important contentof this course <<<<

Havban.wordpress.com