PHP tutorial

Authors: Kirill Mourzenko and Hoa Nguyen
Updated by Arnold Rosenbloom, 2013, 2014
Note: To be done and submitted from the lab.
Forgot your sunfire account? Go to https://mysoc.nus.edu.sg/~myacct/resetpass.cgi References: php lecture notes
Notes: You can check php errors in /var/log/apache2/error.log on cp3101b-1.comp.nus.edu.sg grep -e 'USERID/public_html/guessGame' /var/log/apache2/error.log
In this tutorial you will create a PHP script that will choose a number and have the user try to guess it. Your script should also print out the guesses the user made so far and if they were too low or too high.

To do this you will need to be able to read user input from the form and use PHP's sessions.
  1. Create directory guessGame inside your public_html directory arnold@cp3101b-1:~/public_html$ mkdir guessGame arnold@cp3101b-1:~/public_html$ chmod 711 guessGame arnold@cp3101b-1:~/public_html$ ls -al guessGame total 8 drwx--x--x 2 arnold ssl-cert 4096 Jan 24 14:29 . drwx--x--x 7 arnold ssl-cert 4096 Jan 24 14:29 .. All of your work for this lab will be placed in the guessGame directory.
  2. Create a PHP script called hello.php, which returns a page that simply says Hello.
    Make sure the permissions on the file are set to read/write for the owner only.
    You can do this by executing this command in the shell:
    arnold@cp3101b-1:~/public_html/guessGame$ chmod 600 hello.php arnold@cp3101b-1:~/public_html/guessGame$ ls -al total 12 drwx--x--x 2 arnold ssl-cert 4096 Jan 24 14:33 . drwx--x--x 7 arnold ssl-cert 4096 Jan 24 14:30 .. -rw------- 1 arnold ssl-cert 27 Jan 24 14:33 hello.php To view your script go to this address in your browser: http://cp3101b-1.comp.nus.edu.sg/~USERID/guessGame/hello.php
  3. Create a file named guessGame.php, which doesn't have any PHP code in it yet, but has the HTML for the submittion form. The action attribute of the form should be set to guessGame.php.
    Your page should look like this:
  4. Inside this same file write PHP code that echos back what the user enters in the input field.

    Hint: Use the $_REQUEST array to access user input in your PHP script.
    Look at http://www.php.net/manual/en/ for more extensive documentation on PHP.
  5. Have your script choose a random number and then echo it back.

    Hint: Use the rand function (inside the Math section) to choose a random number.
  6. Change your script, so that it stores the random number in the user's session. In this step you'll need to use PHP sessions. For more details about how to use them go to:
    http://www.php.net/manual/en/ref.session.php. Or take the examples in the php lecture notes.
    Hint: session_save_path, session_start, and, $_SESSION were the keys. You need a directory to keep your users sessions as well. Why?
  7. Now make your script take input from the user using the form and compare it to the random number it has stored in the session. Have it tell the user if the number was higher or lower.

    Hint: To check if user's input is a number use the is_numeric function.
  8. Have your script store the history of all the guesses the user made and the number of guesses the user made so far.

    Hint: Store the history of guesses inside the session as one big string or add an array to the users session.

    This is sample output of the script:
  9. Have your script recognize when a user wins and then restart the game.
  10. When you are finished, zip up your guessGame directory and submit guessGame.zip to the new cp3101b submit page. We may do a code review with your submission, so make sure that there is no identifying information in your submission. zip -r guessGame guessGame