Session 1: The links project is due on Session 2, Week 6. This project and the guestbook project is what will determine your midterm grade, please add any finishing touches before Session 2. Most of you have this included already, but to add a dynamically generated drop down list of categories to the add link form use the following code. Make sure that you place the code within the form tags of the add link form:
<select name="category">
<?
while ( list($key,$value) = each($links_hash) ) {
echo( "<option value=\"$key\">$key</option>\n" );
}
reset( $links_hash );
?>
</select>
To turn in the links project, on the drop off drive (Z:/John Keston/mm2421/<yourname>/links) place the following:
- An Internet Shortcut to your “links” project
- All the source code
- The “links.txt” flat file
- Add at least 5 categories with a total of at least 20 links to your page
- Add some personal design choices to your links page
The next project is called the News Database project and involves making a web interface to a remote database. We’ll use MySQL which is the database platform, and PHPMyAdmin as our tool for managing the database. Add the following tables to your student webspace database. Add at least one user with username and password to the users table:
CREATE TABLE news_users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(64) NOT NULL UNIQUE,
password VARCHAR(64) NOT NULL,
first_name VARCHAR(64) NOT NULL,
last_name VARCHAR(64) NOT NULL,
email VARCHAR(128),
status VARCHAR(32) DEFAULT 'enabled'
);
CREATE TABLE news_articles (
id INT PRIMARY KEY AUTO_INCREMENT,
headline VARCHAR(128) NOT NULL,
author VARCHAR(128),
body TEXT NOT NULL,
create_date DATETIME NOT NULL,
modify_date DATETIME,
user_id INT NOT NULL,
status VARCHAR(32) DEFAULT 'enabled'
);
Session 2: The links project is due today. Next step is to write the code which authenticates the user and sets a session cookie so that we know who has authenticated and use their ID to track who’s creating the news stories. Place these examples in a sub-directory of your php directory called “news”. Next create a login form then create a php script called “auth_user.php” to compare what the user enters to the database.
1. The “Login” form:
<?
////////////////////////////////
// Script Name: auth_user.php //
////////////////////////////////
import_request_variables("gp");
// Connect to a MySQL database
$dbh = mysql_connect ("localhost", "jck362aii_jck", "abc123")
or die ('I cannot connect to the database.');
mysql_select_db ("jck362aii_news");
// Build the database query
$query = "SELECT * FROM news_users WHERE ";
$query .= "username = '$username' AND ";
$query .= "password = MD5('$password') AND ";
$query .= "status = 'enabled'";
// Execute the query and fetch the results
$result = mysql_query($query);
$row = mysql_fetch_array($result);
// check for a match
if ( $row ) {
// Set a cookie and display the add news form
setcookie( "user_id",$row['id'] );
header( "Location: add_news.php" );
exit();
}
// Return to login.php and displays auth failed message
header( "Location: login.php?" .
"message=Authorization failed. Please try again." );
?>