Project 4 Examples

You can login here using the username alice and the password shared in lab

Project 4: Login, Homepage, Insert Question

Add Admin User

add_admin_user.php

This is a URL-key-protected script that allows a developer to add an admin user. Note that you can pass the username and password in the URL so you can easily add admins. Also note that it will print the MySQL error if the query fails. It will fail if the username already exists.

Home

home.php

This shows the most basic session-protected PHP script. Notice that it includes no HTML output. Instead we call functions to print the HTML. View functions.php below to see how the code for the functions.

Login

index.php

This script implements the login authentication where it sets several session variables. This script can either call print_login or print_login_bs, which demonstrates how you can develop a basic form or a more complex form that are independent of the program logic.

Insert Question

insert_question.php

This script implements insert question and shows how the query can be "built" by iterating over the $_POST array to create the field and value lists. Similar to the other scripts, I first implemented a basic form (print_insert_question_form) but the created a Bootstrap form witha multicolumn responsive layout.

Logout

logout.php

Logout is a great example of a script that does not need input and produces no output. The script sole function is to destroy the session and redirect the user to the login page.

Functions

functions.php

This file contains the functions for the login page, home page and insert question page. Note how it is now organized with plenty of comments. As my application grows, I may want split my functions into two or more files. Notice that my individual PHP scripts contain almost all the application "logic" and that these functions contain all the HTML generation. This illustrates how HTML and PHP can be separated.