Project 3

Due Monday, Nov. 30th, 2009 by 4:00pm

Partners

Please remember the ongoing rule about partnerships. The CS143 project may be completed individually or in teams of two, the choice is up to each student. Partnership rules consist of the following:

Please make sure to submit only one copy of the assignment if you work in a team.

Part 1

The primary purpose of this first "PHP warm-up" part is for us to provide you with a whole bunch of basic information, and to get everyone up-to-speed on our computing systems and the languages and tools we will be using. Those of you who have done some Web programming before, especially in PHP, may find this project part nearly trivial. Those of you who haven't will find it merely straightforward. With all that said, please don't start at the last minute -- as with all programming and systems work, everything takes a bit of time, and unforeseen snafus do crop up.

  • Step 0: Getting started with PHP on SEASNet

    Please note that the Apache httpd is listening on port 9143.  httpd is running on lnxsrv03, but there is also the alias cs143.seas.ucla.edu. Specifying either lnxsrv03 or cs143 will get you to the same server. User's web directory is ~/public_html.  In order to allow access to that directory you must do the following: Creating a simple HTML:
    Creating a simple PHP script: Note on PHP: All CGI scripts, including PHP, must have the extension .cgi because this is enforced in httpd.conf.  PHP is used as CGI (not as httpd's module) to enhance security by using suEXEC, http://httpd.apache.org/docs/2.2/suexec.html Although running PHP as an Apache's module provides for more convenient coding style, it is not allowed because we cannot use suEXEC in this case.

  • Step 1: Review the W3CSchools PHP tutorial to learn basic PHP. Please read at least up to "PHP $_POST" page of the tutorial. You can test the examples in the tutorial by creating a php page in the ~/public_html directory. All files in ~/public_html are served by the guest Apache server and are accessible at http://cs143.seas.ucla.edu:9143/~cs143xxx/ from your host browser. Note that cs143xxx should be replaced by your username.

  • Step 2: Connecting to the database

    Connecting to a database in PHP is simple. For this lab, the database set up is MySQL, so you will need specific syntax for that library. To connect to the database, at the beginning of your php script you should have the following lines:

    #!/usr/bin/php
    <?php
    print 'Content-type: text/html' . "\n\n";
    $username="USERNAME";
    $password="PASSWORD";
    $database="DATABASE";
    mysql_connect("localhost",$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    ?>

    Clearly you must replace USERNAME, PASSWORD and DATABASE with your class username, password and database. Open up your PHP script in a web browser. For example if you named your script script2.cgi, you would enter http://cs143.seas.ucla.edu:9143/~cs143xxx/script2.cgi. The script should show nothing at all, most importantly it shouldn’t say “Unable to select database”.

  • Step 3: Forms

    You will need to write several forms in this project. A form is similar to a function call. Each of the inputs is a parameter and the function that’s called is the action. This is an example of a simple form echoed out in PHP:

    echo " <form method='post' action='script3.cgi'>
    <input type='hidden' name='arg' value='begin'>
    <input type='submit' value='Go'>
    </form> ";

    This form will load the script script3.cgi with a parameter of ’arg’ set to ’begin’. To retrieve this parameter in your php script you can do the following:

    NOTE: This is a work-around, because due to security reason the global variable $_POST is disabled on SEASNet.

    // POST data come to "stdin" with running php as cgi, not "input"
    // $_POST is not set when running PHP as CGI
    $post_string = file_get_contents('php://stdin');
    print 'Post string = ' . $post_string;
    $post_array = split ('&', $post_string);
    // Now create our own $_POST array
    foreach ( $post_array as $parameter ) {
    $param_array = split ('=', $parameter);
    $_POST["$param_array[0]"] = $param_array[1];
    }
    $param1 = $_POST['arg'];

    This will put the value from ’arg’ into the new variable param1. You can use this to check different task values. For example if ($param1 == ’begin’) {}... will check the value of param1. You will have to write several of these forms throughout this project.

  • Step 4: Queries in PHP

    Queries in PHP are the same as the command line queries you executed in Project 1. Only difference is, the actual query will be stored in a string variable, and then executed using a specific php function. Here’s an example of how to execute a query:

    $query = "SELECT first FROM actor";
    $result = mysql_query($query) or die("Error");


    Retrieving results from queries is also simple. The resulting table is returned as a series of associative arrays. Here’s how to go about retrieving data from your query. Assuming the query has already been executed, and its result stored in $result...

    while ($row = mysql_fetch_row($result))
    { $actor = $row[0]; print $actor."<br/>";
    }


    This will echo (or display) all the fields associated with actor's first name in each row in your actor table. In this part you are to write several forms, that will call the same function in your PHP script, with different variables. Once eveyrthing is written, this is the database-oriented functionality expected from your script:

  • Some kind of a simple search through your database, with ability to display the results (For example a form that allows you to search for actors by DOB)
  • Ability to remove results that have been searched for (For examples delete actors with specified DOB)
  • Ability to add a new row to your table (Any table you choose)
  • Part 2

    Web functuanality

    The primary purpose of this part is to create a functional webpage where a user can login as well as rate movies and administrator can manager user accounts. All of this is to be done using PHP.

  • Step 0: Logging in and rating movies

    As mentioned before, in this project users are expected to both be able to login using the accounts that they create and also be able to rate movies. The database stores average rating of a movie based on up-to-date rating information. For users to rate movies, they need to register for the site and create logins for themselves. After they login to the site with the correct user name and password, they can rate movies. A user can rate a movie in the range of 1-10. Every time a new rating is generated, the average rating for the movie is updated in the database. (Hint: you may want to store the average rating of a movie and the count of users who have rated this movie so far for such updates ). It is up to you, the owner of the database, to decide if you allow a user to update/delete his rating of a movie.

  • Step 1: Interface for administrator

    The database administrator is a special user with the user name “admin”. He has unrestricted access to all the data in the database. Besides the search interface for regular users, you web site should also support the direct input of SQL queries for use by the administrator. Information about the movies, actors, actresses, directors, and mpaa ratings in the movie database can only be updated with an administrator login. (Hint: you can use the same SQL interface for both searches and updates by the administrator.)

  • Step 2: Constraints

    Updating the database
  • After the initial loading, only the administrator is allowed to add tuples representing new movies, new actors, new actresses, or new directors to the relevant tables, and to update or delete existing tuples about movies, actors, actresses, and directors.
  • The database can also be updated with the information of user login or rating when a user registers to the site or rates a movie.
  • Need of user login
  • A user can search the database without having to login. But login is needed to be able to rate movies.
  • Rating movies
  • To ensure fair rating, each user is allowed to rate a movie only once. In addition, a user under the age of 18 is only allowed to rate certain movies based on the mpaa-ratings of a movie and the user’s age. Use this chart as a guideline of mpaa-ratings.

  • Part 3

    Queries

    The primary purpose of this exercise is to make sure that your web application can express various types of queries.

  • Step 0: Query support
  • Step 1: Express the following queries using your interface
  • Late Submission Policy

    To accommodate the emergencies that students may encounter, each team has 3-day grace period for late submission. The grace period can be used for any part of the project in the unit of one day. For example, a student may use 1-day grace period for Project 1 and 2-day grace period for Project 2. Any single project part may not be more than 2 days late. Note that even if a team submits a project 12 hours late, they would need to use a full day grace period to avoid late penalty. If your project is submitted late, we will automatically use the available days in your grace period unless you specifically mention otherwise in the README file.

    What to Submit

    Please submit the following files electronically (no hard copy):