Logo  

CS479/579 - Web Programming II

Description

Exercise #5

Note: You will need to have your own webserver to run this on, you may need to install Apache w/ PHP in order to do this exercise.

Create a PHP script called mboard.php that implements a simple message board that is saved to a file called 'messages' in the same directory as the PHP script. Each message should have a title and a message body. Messages should be saved to the messages file as a JSON encoded array of messages.

Note: in order for the web server to write to the file the file should either be made world writable (chmod 666 messages), or an ACL allowing the web server to write to it should be added (setfacl -m u:apache:rw messages). Do this after creating an empty file (touch messages).

Example JSON contents w/ two messages (with some extra white-space to make it more readable here:

[
 {"title":"Title #1","mesg":"Test message #1"},
 {"title":"Title #2","mesg":"Test message #2"}
]

The mboard.php should have a form that posts to itself in order to add another message to the message file.

You should consider running the solution to see how the exercise should operate. How it looks is of no importance, only the function is important for purposes of this exercise.

PHP functions you may need to know to complete this assignment, read more about them on php.net:

json_decode(string $str, true)

  • Decode JSON string as associative array second parameter == true then decodes JSON objects as PHP associative arrays.

json_encode(mixed $value)

  • Encode data as JSON string.

file_get_contents($filename)

  • Returns file as a string.

file_put_contents($filename,$data)

  • Saves $data to $filename

Optionally:

fopen($filename, $mode)

  • Open a file

fwrite($fhandle, $string)

  • Write string to a fopen'ed file.

fclose($fhandle)

  • Close opened file

htmlspecialchars($string)

  • Escape special HTML characters in a string.

gettype($var)

  • Get the type of a variable

array_push($array, $value)

  • Push $value onto the array $array or $array[] = $value

count($array)

  • Get number of elements in an array.