Make a copy of the jspractise folder and name it phppractise. Change the extension of the four HTML files to .php.  If you do not see the extension you need to show it (press Alt on your keyboard to show the menu and go to Tools - Folder options, View tab and untick Hide known file extensions).

Your client wants you to add the use of PHP to the pages by doing what is described below.


On the first page they want a different quotation each day.  They will provide the quotations by uploading a new one in a text file every day.  They want you to get the page to read it from the text file and display it on the page:

  1. download this file (right click and save as) and upload it to your server for testing (it holds a single example quotation)
  2. add some PHP below the list of products (but before the </body> tag):The quotation in a styled blockquote element
    1. read the file contents into an array using file()
    2. print_r() the array to check it work then delete the print_r line
    3. put the first (and only) element of that array into a variable
    4. echo a blockquote element complete with the text from the variable
    5. add styles to give the blockquote a border, extra margins and padding so that it looks something like this:


They want the messages sent using the HTML form to be saved in another file.  Start by preparing the data:

  1. create a new, completely empty (blank with no code at all) PHP page to handle the processing of the form data and call it formprocess.php
  2. add the <?php and ?> (you don't need any HTML as it is not going to display anything)
  3. open address.php for editing and change the action of the form to formprocess.php
  4. on the new page add print_r() $_POST to check that the form is working (it should show something like this when the form is submitted: Array ( [name] => me [email] => [message] => Hi ) )
  5. delete that print_r line as it was just for testing
  6. get the data from $_POST and put each of the three parts into three elements of an array called $message with three associative indexes name, email and message
  7. get the users IP address ($_SERVER["REMOTE_ADDR"]) and put that into the array with the index ip (so the array now has four elements)
  8. get the time the form was posted ($_SERVER["REQUEST_TIME"]) and put that into the array with the index time
  9. use print_r() to quickly test that the array contains all the correct info which should look something like this:

Array ( [name] => me [email] => [message] => Hi [ip] => [time] => 1466412707)

The time is the number of seconds since 1st January 1970 (known as UNIX time).  Now write that data to the file:

  1. delete the print_r line if you didn't already
  2. use the array to write the data to a file which will end up looking like the one shown here:
    1. open the file log.txt for writing
    2. write the first array element
    3. write a tab using a new line of code (\t)
    4. write the second array element then write a tab
    5. write the third array element then a tab
    6. write the fourth array element then a tab
    7. write the fifth array element then a carriage return (\r\n)
    8. close the file
  3. test by storing a few messages and viewing the file from the server in your FTP client or browser.

Be careful when testing not to type special characters into the form fields as you have not learned how to automatically escape them yet.  Single and double quotes are worst but also $ and \.