Things to start the lab with today: 1) Go to http://www.cs.indiana.edu/classes/a202 2) Click on What's New? 3) Click on the link to the source code for the first program of yesterday: http://silo.cs.indiana.edu:8346/1008/one.phps 4) Copy the code and place it in ~/apache/htdocs/thu01.php 5) Check that the program is working. 6) Click on the link to the source code for the second program of yesterday: http://silo.cs.indiana.edu:8346/1008/two.phps 7) Copy the code and place it in ~/apache/htdocs/thu02.php 8) Check that this program is working too. 9) Question: What is different between the two? (How do they not behave the same?) Three answers: a) the URLs are different (one has the state the other one doesn't) b) windows from the same browser share the state when sessions are used (same ID) c) reload makes them act differently Access of a file on a web server a) if the file is Python script http:// protocol silo.cs.indiana.edu host :8346 port number /cgi-bin/ this is a convention script alias (see httpd.conf) lab6/whatever the name of the script b) if the file is an .html file http:// protocol silo.cs.indiana.edu host :8346 port number / this is a convention (we don't write htdocs) lab6/whatever the name of the script How would you convert this program to PHP: #!/usr/bin/python def sum(los): if len(los) == 0: return 0 else: try: value = int(los[0]) + sum(los[1:]) except: value = sum(los[1:]) return value print "Content-type: text/html\n\n" import cgi, random data = cgi.FieldStorage() (deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo, feedback) = ("", "", "", "", "", "", "", "") if data.has_key("deck"): deck = data["deck"].value if data.has_key("deckOne"): deckOne = data["deckOne"].value if data.has_key("deckTwo"): deckTwo = data["deckTwo"].value if data.has_key("handOne"): handOne = data["handOne"].value if data.has_key("handTwo"): handTwo = data["handTwo"].value if data.has_key("scoreOne"): scoreOne = data["scoreOne"].value if data.has_key("scoreTwo"): scoreTwo = data["scoreTwo"].value if deck: deck = deck.split(",") deckOne = deckOne.split(",") deckTwo = deckTwo.split(",") handOne = deck[0] handTwo = deck[1] if int(handOne) > int(handTwo): deckOne[0:0] = [handOne] deckOne[0:0] = [handTwo] feedback = "(%s, %s) Dilbert's hand" % (handOne, handTwo) elif int(handTwo) > int(handOne): deckTwo[0:0] = [handOne] deckTwo[0:0] = [handTwo] feedback = "(%s, %s) Ratbert's hand" % (handOne, handTwo) else: deckOne[0:0] = [handOne] deckTwo[0:0] = [handTwo] feedback = "(%s, %s) Tie" % (handOne, handTwo) scoreOne = sum(deckOne) scoreTwo = sum(deckTwo) deck = deck[2:] deck = ",".join(deck) deckOne = ",".join(deckOne) deckTwo = ",".join(deckTwo) else: feedback = "Welcome to the game." deck = [] for i in range(13): for j in range(4): deck.append(str(i)) random.shuffle(deck) deck = ",".join(deck) scoreOne = "0" scoreTwo = "0" print """
%s

Deck: %s

Player Current score Hand Player Deck
Dilbert: %s %s %s
Ratbert: %s %s %s

Press to move on.

""" % (feedback, deck, scoreOne, handOne, deckOne, scoreTwo, handTwo, deckTwo, deck, deckOne, deckTwo, handOne, handTwo, scoreOne, scoreTwo)