PHP tutorial

Authors: Kirill Mourzenko and Hoa Nguyen
Updated by Arnold Rosenbloom, 2013, 2014, 2015
Updated by Larry Zhang, 2016

Notes: You can check php errors in /var/log/httpd/ssl_error_log on cs.utm.utoronto.ca grep -e 'USERID/www/guessGame' /var/log/httpd/ssl_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 www directory cd ~/www mkdir guessGame chmod 711 guessGame ls -al guessGame total 3 drwx--x--x+ 2 rosenbl6 lecturers 2 Jan 20 22:12 . drwx--x--x+ 8 rosenbl6 lecturers 9 Jan 20 22:12 .. All of your work for this lab will be placed in the guessGame directory.
  2. Create a PHP script called hello.php in your guessGame directory. hello.php 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:
    chmod 644 hello.php ls -al total 6 drwx--x--x+ 2 rosenbl6 lecturers 3 Jan 20 22:14 . drwx--x--x+ 8 rosenbl6 lecturers 9 Jan 20 22:14 .. -rw-r--r--+ 1 rosenbl6 lecturers 26 Jan 20 22:14 hello.php To view your script go to this address in your browser: https://cs.utm.utoronto.ca/~UTORID/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 from the lecture.
    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.