PHP scripts must have the extension .php They need to be placed in htdocs They read data as follows; this sequence of steps: use CGI; $q = new CGI; $something = $q->param("something"); is automatically accomplished by PHP each time. Let's develop this: http://silo.cs.indiana.edu:46016/0218/two.php Here's how we do it: State in the program will consist of two variables a) $balance b) $times Which one of these two should we check to see if we have state? Answer: $times Here's the program: // state is $balance and $times, which are read automatically if ($message && ! $reset) { if ($direction == "Up") { $balance += 1; } else { $balance -= 1; } $times += 1; } else { // no state, initialize $balance = 0; $times = 0; } $message = "You have pushed the button(s) $times times thus far."; ?>
echo $balance ?> Note the three buttons, one of which is the Reset button. Note the message state variable that is more robust than an integer variable (because of 0 checking out as "false" in a boolean context). So what does it take to move the state on the server side: 1. Put a session_start(); at the top of your program. 2. Replace each hidden field with a session_register call. So the program becomes: session_start(); // state is $balance and $times, which are read automatically if ($message && ! $reset) { if ($direction == "Up") { $balance += 1; } else { $balance -= 1; } $times += 1; } else { // no state, initialize session_register("message"); session_register("times"); session_register("balance"); $balance = 0; $times = 0; } $message = "You have pushed the button(s) $times times thus far."; ?>
echo $balance ?> We keep an eye on the session folder: -bash-3.2$ pwd /u/dgerman/apache/phpsessions -bash-3.2$ ls -l total 0 -bash-3.2$ As we start working with the server-side state program we see: -bash-3.2$ ls -l total 8 -rw------- 1 dgerman faculty 85 Feb 19 14:13 sess_c11d385f3434d08b75ca095df95ba94b -bash-3.2$ cat sess_c11d385f3434d08b75ca095df95ba94b message|s:47:"You have pushed the button(s) 2 times thus far.";times|i:2;balance|i:2;-bash-3.2$