Keeping state on the server (PHP sessions)
using CGI/Perl (so, sessions with Perl, using CGI?!)
No default way to store state on the server side because
HTTP is connectionless and that means that
a) it happens in pairs of request-response
b) pairs are independent entirely
-bash-3.1$ telnet silo.cs.indiana.edu 9306
Trying 129.79.247.5...
Connected to silo.cs.indiana.edu (129.79.247.5).
Escape character is '^]'.
GET /index.html HTTP/1.0
HTTP/1.1 200 OK
Date: Tue, 12 Feb 2008 18:21:53 GMT
Server: Apache/2.2.2 (Unix) PHP/5.1.4
Last-Modified: Tue, 12 Feb 2008 18:18:43 GMT
ETag: "c0dc338-5b-162c86c0"
Accept-Ranges: bytes
Content-Length: 91
Connection: close
Content-Type: text/html
It works!
This file belongs to Adrian.
Connection closed by foreign host.
-bash-3.1$ hostname
bobac.cs.indiana.edu
-bash-3.1$
Minute paper: follow the lecture as closely and write down
questions about the various parts, modules so I can look at
those and maybe discuss them on Thursday.
1. We first need a way to identify (uniquely) our customers.
When the data is with the client (hidden, links) the client has
it, so you don't need an id, the client/data is it, and you send
the changed data with the client.
When you store the entire data for everybody on the server you
need to create something like lockers, which need to be opened
with keys. So each user will have a key: session id, and that will
open only the data repository (session data) for that user.
Pay attention to the redirection. We will talk more about hash
later, when it will mean something. Here it just scrambles letters
and digits to come up with a (well-formed) random string.
2. Implement our template
a) retrieve state (and input)
what is state: message, flags, good, total, key; answer
here we will use MySQL and define a table for all the sessions
associated with a program (like a specific phpsessions)
how was I retrieving state earlier? $q->param('message) the user
was bringing then state with him/her
how am I going to do it now? first see who the user is, so get the
session id first then match that with the entry in the table for the
program and there will be a row with that session id (key in the table)
and that row will have a collection of values, one per column (each
column represents a variable in the state)
b) is state empty?
if yes: initialize state
else: take input into account and update state
net result: brand new state
c) save state
earlier: print the values in the form as hidden fields
now: update record in the table
d) report state
same as before
e) get ready for more input
same before, no hidden fields
-bash-3.1$ cat client_connect
mysql --socket=/nobackup/dgerman/mysql/mysql.sock \
--port=57862 --host=silo.cs.indiana.edu -u dgerman -p
-bash-3.1$ ./client_connect
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 40 to server version: 5.0.22-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use demoOne;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> edit
UW PICO(tm) 4.10 File: /tmp/sqldsDPbP Modified
create table example (
session_id char(8) primary key,
message char(80),
secret int,
attempts int,
won int,
lost int,
modified timestamp
);
[ Wrote 9 lines ]
-> ;
Query OK, 0 rows affected (0.03 sec)
mysql> describe example;
+------------+-----------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-----------+------+-----+-------------------+-------+
| session_id | char(8) | NO | PRI | NULL | |
| message | char(80) | YES | | NULL | |
| secret | int(11) | YES | | NULL | |
| attempts | int(11) | YES | | NULL | |
| won | int(11) | YES | | NULL | |
| lost | int(11) | YES | | NULL | |
| modified | timestamp | YES | | CURRENT_TIMESTAMP | |
+------------+-----------+------+-----+-------------------+-------+
7 rows in set (0.00 sec)
mysql> select * from example;
+------------+---------+--------+----------+------+------+---------------------+
| session_id | message | secret | attempts | won | lost | modified |
+------------+---------+--------+----------+------+------+---------------------+
| 1ae884cd | NULL | NULL | NULL | NULL | NULL | 2008-02-12 14:14:08 |
+------------+---------+--------+----------+------+------+---------------------+
1 row in set (0.04 sec)
mysql> select * from example;
+------------+----------------------------------------------------------------------------------+--------+----------+------+------+---------------------+
| session_id | message | secret | attempts | won | lost | modified |
+------------+----------------------------------------------------------------------------------+--------+----------+------+------+---------------------+
| 1ae884cd | NULL | NULL | NULL | NULL | NULL | 2008-02-12 14:14:08 |
| 93f51d54 | Game reset, you lost the game The number was 17 The current score is: (0 - 1) | 41 | 0 | 0 | 1 | 2008-02-12 14:16:13 |
+------------+----------------------------------------------------------------------------------+--------+----------+------+------+---------------------+
2 rows in set (0.00 sec)
mysql>