CS 328 - Week 9 Lecture 1 - 2016-03-21
* (did a brief review of finite state machines --
which, it turns out, can be useful for modeling/designing
the flow of "pages" within a web application!)
* point of fact:
plain, vanilla HTTP (hypertext transfer protocol)
is STATELESS
* based on request->response
* in PLAIN VANILLA HTTP, there is NO way to
associate one request with a *previous* request;
^ there ARE a variety of KLUGES that have devised
to "tack on" state info,
because user experiences are improved with
state info!
^ and many application-tier languages
include support for one or more of these
kluges "built in";
* ...and yes, this includes PHP.
* another superglobal associative array:
$_SESSION
* usually one should NOT muck with the contents
of a PHP superglobal associative array,
BUT $_SESSION is a definite exception to this
* you (as an application-tier programmer)
can ADD as many new keys and values
to $_SESSION as you would like;
* NOTE: a key in a $_SESSION array
is often also called a session ATTRIBUTE
* so, the application programmer is free
to add session attributes ($_SESSION
keys), and set their values,
and retrieve them as well;
* IF you want to use $_SESSION for state info,
and you are using the PHP default of cookie-based
sessions (which we are),,
then, the FIRST thing in your PHP document,
YES, EVEN BEFORE the <!DOCTYPE...!
is a call to a function session_start();
<?php
session_start();
...
...and now you can use $_SESSION as desired to
save and use state info from earlier in a logical
session;
* simple example: see try-session1.php
* BUT, usually if we are using sessions,
we want a more sophisticated "flow";
* What if we structure a PHP document using
sessions to create a multi-page application
as follows:
<?php
if (at the beginning)
{
do_first_thing();
$_SESSION['next_step'] = 'step 2';
}
elseif ($_SESSION['next_step'] == 'step 2')
{
do_second_thing();
$_SESSION['next_step'] = 'step 3';
}
elseif ($_SESSION['next_step'] == 'step 3')
{
do_third_thing();
$_SESSION['next_step'] = 'step 4';
}
...
elseif ($_SESSION['next_step'] == 'last step')
{
do_last_thing();
session_destroy();
}
?>
* we'll give this approach a try next time;