Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Homework 5 - PHP Connections

Due: Thursday, March 28, 2024 at 11:59pm


  • Gain experience writing in PHP using objects and sessions


For this homework, you may work alone or with another student in this course. You will implement a Connections-like game that picks 4 categories which each have 4 associated words, displays the 16 words shuffled in a 4x4 table, and requires the user to guess which words belong to the same category. It then provides feedback on if the user guessed correctly or how close their guess was. Users will be prompted to enter their name and email, they will be able to see their prior guesses, and their sessions will be tracked using $_SESSION. You may use the in-class Trivia Game as a starting point, or you may implement everything from scratch.

Connections Requirements

Move over Connections… we’re writing our own category guessing game! Our web application will begin by displaying a welcome screen that requires users to enter their name and email, which we’ll use to personalize the game interface. Once they enter that information, the game page will be displayed. On the game page, the user will be presented with a 4x4 table of words or phrases and a text box to ether their guess. Each word in the table will have an associated number that the user must enter; they’ll enter them space separated in the text box. Note: we’ll make this much more user friendly in a future assignment! Once the user enters a guess, if they are incorrect, the game page will be displayed again, showing their previous guesses and how close they were. That is, if at least two of words (or phrases) the user guessed are in the same category, we’ll tell the user how many of the other words (or phrases) in their guess were not part of the category. Once a user guesses all the words in a particular category, those words (or phrases) are removed from the table and the table shrinks by one row. But remember, we will still show them all their prior guesses. Users may continue guessing until they guess all the words (or phrases) in all the categories, at which point they will be taken to a Game Over screen displaying the results of their game.

For this assignment, you must implement the following components. (You may implement more functionality if you wish, for example highlighting the table cells of their prior guess, but remember that we’ll be grading on effort in implementing these components below.)

  1. Front controller
    • Create an index.php file, which will receive all of the requests for your application. It should instantiate and run your category guessing game’s controller as well as handling any necessary session management.
    • Create a CategoryGameController class that contains the controller to handle all of the logic for your application.
  2. Your application should have at least the following 3 views/templates available to the user. You may choose to include more.
    1. Welcome page. It must ask the user for at least their name and email.
    2. Game page. This template will display the main game page. It should have the following content:
      • The user’s name, email, and current number of guesses.
      • Instructions asking the user to enter the numeric ids of their guess (space separated).
      • A form and text box for the user to enter the guess, with a button to submit.
      • A list of all the user’s prior guesses. Please be creative! You are encouraged to determine how best to organize the display, but for each prior guess, the application must display:
        • which words (or phrases) they guessed, and
        • how many words (or phrases) were not in the group if they had at least 2 correct or some text noting that their guess does not contain enough words from any category.
        • For example, if the user chose “red” and “blue” from a “colors” category and “orange” and “blue” from an “about UVA” category, then the page must state that the user was close, but missing two phrases.
      • An option (link, button, etc) for the user to quit the game, which will take the user to the Game Over page.
    3. Game Over page. This template will display the list of categories and their words, the number of guesses it took to select all categories correctly (if they were successful), and provide the option for the user to play again or exit. If the user chooses to play again, return them to the game page with a new set of words (from new categories). If the user chooses to exit, destroy the current session and display the welcome page again.
  3. You must implement the following logic for the overall game:
    • Read in the category list from our CS4640 Category Bank JSON file. Updated! You should save this JSON file locally in your src directory (so that it won’t be publicly available to view under Apache).
      • Category bank (for development): JSON File
      • When you publish your code to the CS4640 server, update your code to read our connections.json file directly from the server. You can do this by reading from the following absolute path: /var/www/html/homework/connections.json
    • Your application should choose four categories at random from the bank and use them as the categories and words for the game. You should then choose four words at random from each of those categories.
    • When the user makes a guess, it should be compared with the words in the game’s categories. Note that you will need to store the mapping of index to word and compare words.
  4. For portability between our local Docker enviroment and the cs4640 server, you should use query string variables ($_GET variables) to pass commands to the controller rather than using Apache’s mod_rewrite and a .htaccess config file. That is, you should not need a .htaccess file.
    For example, you may wish to use the command variable to determine the current view; sending the user to index.php?command=welcome would then display the welcome page. The Controller may then determine logic to take based on the $_GET["command"] value.
    Note: you may use mod_rewrite, if you wish, but it may lead to unexpected results. It is discouraged for this assignment.
  5. User information and guess history for the current session should be stored in thee $_SESSION array. You may choose to determine the best way to store this data, but remember that it uses key-value pairs. Hint: Consider using JSON to store PHP arrays.
  6. Create any other classes that you need to help with your application.
  7. Session tracking
    • The user should not be able to access any other views except the welcome page unless they have provided their name and email.
    • You must keep track of the current session with $_SESSION. When the user ends the game, destroy the current session and display the welcome page again.


The submission will consist of two parts:

  1. Submit your code (HTML, CSS, PHP, etc) to Gradescope in the “Homework 5” submission. You may choose to upload the files individually, upload a zip folder, or upload from a GitHub repository.
    • You must include a link to your published version in the comments of your index.php page, or you will not receive credit!
  2. Publish your web site to the cs4640 server under a folder titled “hw5”. Therefore, we should be able to access your work at If you work with a partner, each person should publish the work to their web space.

Grading Rubric

This homework is worth 50 points, based on the following rubric:

  • 10 points - Front controller and URL rewriting
  • 10 points - Maintain session with $_SESSION
  • 5 points - Welcome template
  • 10 points - Game template and submission logic
  • 10 points - Game Over template and display logic
  • 5 points - Publishing your site to the cs4640 server

Academic Integrity

For this assignment, you are welcome to share/publish your website! You’ll be doing that on our cs4640 server as well. We only ask that you not make any GitHub projects public until after the late deadline.

Note: You must cite any sources you use in a comment at the top of your index.php file. For example:

// Sources used:, ...


Use of Generative AI

For Homework 5, you are not allowed to use generative AI tools to help you solve this problem.