This assignment is meant to give practice to the most important parts of PHP besides database integration, which is addressed in separate assignments. The core concepts are include files and passing data (a.k.a. persistent data) using forms, links, and cookies.
Your assignment is to make a site consisting of four web pages:
- Home (index.php)
- News (news.php)
- Contact Us (contact.php)
- Thank You (thank_you.php)
There will also be multiple other files that the user never sees directly. These will be:
- process_name.php (process the data the user enters on the Home page)
- process_contact.php (process the data the user enters on the Contact Us page)
- _article1.php, _article2.php, _article3.php, etc (the XHTML snippets for each article linked to from the News page)
Home (index.php)
Global navigation. The Home page, like every other page on the site, has a top navigation menu with links to Home, News, and Contact. This menu should use the technique of creating horizontal menus from list tags. The global navigation should be created as a separate file so it can be included on all other pages of the site in a templatized manner.
- Read also this discussion about templatizing web sites using PHP.
- See this example of creating a templatized top navigation file as an external include file.
Form. The Home page also has a form the user can fill in with their first and last names. This form should submit the data the user enters to a script file called process_name.php via the HTTP POST method, which does something with that data.
- See also the discussion about passing data with forms
- and the example of using a form to submit a HTTP POST request to the server.
The code for the form will probably look something like the following. Note the action and method attributes of the form tag and the name attributes of the two form input elements. These are all important and you must understand why they are there:
<form action="process_name.php" method="POST"> <label for="first_name">First Name:</label> <input type="text" id="first_name" name="first_name" /> <br /> <label for="last_name">Last Name:</label> <input type="text" id="last_name" name="last_name" /> <br /> <input type="submit" value="Enter site!" /> </form>
process_name.php
When the user clicks the submit button on the form on the Home page, the data they entered is sent along with the HTTP POST request for the file, process_name.php.
process_name.php should take the first name that the user entered in the form and store it semi-permanently in a cookie. Setting a cookie is handled by PHP’s built-in setcookie() function.
Once that is done setting the cookie, this script should redirect the user to the News page (news.php). Redirecting is done using PHP’s built-in header() function, which allows you to send customized HTTP headers from the server to the client.
- See this example of using PHP’s header() function to redirect to another page (note you’ll have to download this code from the server using file transfer in order to view it).
News (news.php)
Global navigation. The News page, like the Home page, includes the global top navigation from the external file.
Reading the cookie. The News page also reads the cookie which has the user’s first name in it. To read a cookie, you can use PHP’s built-in $_COOKIE associative array.
- See this example of reading the value of a cookie using PHP’s built-in $_COOKIE array
News articles. Each article title in the list of articles titles on the left side of the News page should be a link to news.php. Along with each link, we will be passing the id of the article the user clicked on using the technique of passing data via links.
The XHTML for the article links will look something like the following. Notice how each link passes a different article_id parameter in the query string:
<ul> <li><a href="news.php?article_id=1">article title 1</a></li> <li><a href="news.php?article_id=2">article title 2</a></li> <li><a href="news.php?article_id=3">article title 3</a></li> </ul>
Loading the correct article. The News page is therefore a template that can be used to load up any article, depending which link is clicked. So, obviously, the code must somehow determine which article to load up.
In the code from the example linked above, you’ll see that we find out which article link was clicked by the user by looking for the article_id parameter that was submitted along with the request for the news.php page in the query string in each link.
$articleId = $_REQUEST['article_id']; //get the article id from the browser's request data
//check to make sure there is an article id that was requested
//is_numeric() is a built-in PHP function that checks to make sure a variable is a number
//the ! is a logical NOT, so this statement says "if the article id is NOT a number, do the following:"
if (!is_numeric($articleId)) {
//if there was no article id requested, use the number 1 as a default
$articleId = 1;
}
Then, depending on which article id was requested, in the body of the XHTML document on the news.php page, we load up a different include file with the correct article content in it.
//load up the article that was requested from the external article content files
if ($articleId == 1) {
//include an external XHTML snippet into this template
include("_article1.html");
}
elseif ($articleId == 2) {
include("_article2.html");
}
elseif ($articleId == 3) {
include("_article3.html");
}
//...and so on for all the other articles
Contact Us (contact.php)
Global navigation. The Contact Us page includes the global navigation the same way as the other pages.
Article links. It also has the set of article links on the left side of the page, the same as on the News page. These should each link to news.php and pass along their corresponding article_id parameters, exactly as described for the News page.
Contact form. The form allows the user to send an email to the site administrators. When a user fills out the form and clicks submit, the form sends a request for the script, process_contact.php, which handles sending the email.
- See also the example of sending an email with PHP, in particular the index.html file.
process_contact.php
process_contact.php, like process_name.php, is invisible to the user – the user never sees it directly. This script takes the data that the user entered into the form on the Contact Us page, and sends it in an email to the site administrators.
Once the email has been sent using PHP’s built-in mail() function, the script then redirects the user to the Thank You page (thank_you.php) using PHP’s built-in header() function, the same way we did it on process_name.php.
- See also the example of sending an email with PHP, in particular the process_email.php file which you will be able to download from the server using a file transfer program.
Thank You (thank_you.php)
Global navigation. This page uses the same global navigation links and same article list as seen on the other pages.
Thank you message. The main content of this page is a message thanking the user for submitting the Contact form.
<form action="process_name.php" method="POST"> <label for="first_name">First Name:</label> <input type="text" id="first_name" name="first_name" /> <br /> <label for="last_name">Last Name:</label> <input type="text" id="last_name" name="last_name" /> <br /> <input type="submit" value="Enter site!" />
</form>













