Today we want to learn how to write our Homework One in PHP. We have PHP, we installed it yesterday. If you create pico ~/apache/htdocs/four.php Put this in the file: Then save and exit. Call the file from http://silo.cs.indiana.edu:44xxx/four.php All your PHP scripts need to be placed in htdocs with extension .php A PHP script is essentially an HTML file that can (but doesn't have to) have code in it. So if you create a file five.php:
What's your name:
Access it from http://silo.cs.indiana.edu:44xxx/five.php Now change it to:
Welcome to silo:

What's your name:

Then call it again to see it reporting the time. To post the code set up a symbolic link: ln -s five.php five.phps Then see the source code on-line: http://silo.cs.indiana.edu:44xxx/five.phps When a form containing a field named "something" sends data to a PHP script the PHP script automatically does all the parsing and the data binding that took us so much in Python and Perl and places the value of the field in a variable with the same name ($something) in the script: To prove this change five.php:
Welcome

Welcome to silo where the time is:

What's your name:

Then access it: http://silo.cs.indiana.edu:44xxx/five.php Then in the form type a name, push submit, verify the name is reported. Now we go back to #!/usr/bin/python import cgi, random input = cgi.FieldStorage() print "Content-type: text/html\n\n" (message, n1, n2, m1, m2, answer) = ("", "", "", "", "", "") if input.has_key("message"): message = input["message"].value if input.has_key("n1"): n1 = input["n1"].value if input.has_key("n2"): n2 = input["n2"].value if input.has_key("m1"): m1 = input["m1"].value if input.has_key("m2"): m2= input["m2"].value if input.has_key("answer"): answer = input["answer"].value if message: if int(answer) == int(n1) + int(n2): m1 = str(int(m1) + 1) m2 = str(int(m2) + 1) message = "The score is " + m1 + " out of " + m2 + " currently" n1 = random.randrange(-50, 50) n2 = random.randrange(-50, 50) else: message = "Welcome" n1 = random.randrange(-50, 50) n2 = random.randrange(-50, 50) m1 = 0 m2 = 0 print """
%s

What is %s + %s ? Answer:

Press to submit your answer.

""" % (message, n1, n2, n1, n2, m1, m2, message) Place this program somewhere make sure it works. We know what it does, how could we write it in PHP? Here's the conversion:

What is + ? Answer:

Press to submit your answer.

This is still five.php so access it or the source code as before. Finally: a) make sure you have ~/apache/phpsessions if not: create it with mkdir ~/apache/phpsessions b) make sure that ~/apache/conf/php.ini at line 892 (or so) has session.save_path = "/u/username/apache/phpsessions" where username is your username (for me: dgerman) Once we restart the server we will be able to use sessions. Let's modify this program to keep state on the server side. So go to ~/apache/htdocs and copy five.php into six.php then open it. I will convert this program to keep state on the server side. 1. Where will the state be? In the folder ~/apache/phpsessions Take a look at it now, it's empty. 2. Why? Because php.ini says so. Restart the server to make sure it knows. ~/apache/bin/apachectl restart 3. Make the following three changes in the program: a) place session_start(); at the top of your code Please call my script at http://silo.cs.indiana.edu:44063/six.php We see that sessions get created. My script now keeps track of you all. Like this: -bash-3.2$ ls sess_10cea6b3e4d6066abca2dcdc3ac0b429 sess_2695dcf75cf5ab7ef4083f00c41c369c sess_359914280060b4a9f11ff91cc69c9868 sess_430fc84ac2b60320400279c7e0d5cdd9 sess_5c11774e7809d5fb05094feedd77a437 sess_7c9978e49515ad55e51c0042f8a92603 sess_9e645098cc2ca367993ce304ba7aaf5e sess_ac190843d67979c9a8c58c958db7cfef sess_e3d1ff7de275be7332be63ebd8379708 -bash-3.2$ b) in the initialization part of your script/template register your state variables with the session (please do not register input variables or output variables, just the session variables). Like so: } else { session_register("message"); session_register("n1"); session_register("n2"); session_register("m1"); session_register("m2"); $message = "Welcome"; $n1 = rand(-50, 50); $n2 = rand(-50, 50); $m1 = 0; $m2 = 0; } c) now remove the hidden fields from your output You're done:

What is + ? Answer:

Press to submit your answer.

I removed all the sessions and I called again. Here's how the session evolved: -bash-3.2$ more sess_9e645098cc2ca367993ce304ba7aaf5e message|s:7:"Welcome";n1|i:10;n2|i:17;m1|i:0;m2|i:0; -bash-3.2$ more sess_9e645098cc2ca367993ce304ba7aaf5e message|s:33:"The score is 1 out of 1 currently";n1|i:-3;n2|i:-14;m1|i: 1;m2|i:1; -bash-3.2$ more sess_9e645098cc2ca367993ce304ba7aaf5e message|s:33:"The score is 1 out of 2 currently";n1|i:14;n2|i:-11;m1|i: 1;m2|i:2; -bash-3.2$ Final question for today: do these two programs that we developed http://silo.cs.indiana.edu:44063/five.php http://silo.cs.indiana.edu:44063/six.php today (one keeps state in hidden fields on the client side and the other one invents and distributes ids and stores state in files in phpsessions with the name the ids generated) work the same or are they different at all? Some answers: a) different are the urls when we work with them http://silo.cs.indiana.edu:44063/five.php?answer=5&n1=-7&n2=12&m1=0&m2=0&message=Welcome http://silo.cs.indiana.edu:44063/six.php?answer=34 b) same is the result, they both correctly grade our answers c) they are different (really different!) in how they handle refresh/reload. Possible issues: if you don't put