CS 85 Homework - Unit 7 - Ian Sikes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ian - Hey Everyone! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Unit 7 - Ian J Sikes</title> | |
</head> | |
<body> | |
<h1>Discussion Board</h1> | |
<?php | |
// If the comment form was submitted | |
if (isset($_POST['author']) && isset($_POST['comment'])) { | |
// Sanitize the inputs | |
$author = test_input($_POST['author']); | |
$comment = test_input($_POST['comment']); | |
if (!empty($author) && !empty($comment)) { | |
// Add the comment to the file | |
append_comment($author, $comment); | |
} | |
} | |
// Sanitize input data | |
function test_input($data) { | |
return htmlspecialchars(stripslashes(trim($data))); | |
} | |
// Display a simple form for posting new comments | |
function display_form() { | |
echo "<form method='post' action='" . htmlspecialchars($_SERVER["PHP_SELF"]) . "'>"; | |
echo "<div>Name: <input name='author'></div>"; | |
echo "<div>Comment: <input name='comment'></div>"; | |
echo "<input type='submit' value='Post comment'></form>"; | |
} | |
// Add a comment to the end of a txt file | |
function append_comment($author, $comment) { | |
// file_put_contents("guestbook.txt", "$author - $comment") or die("Unable to write to file!"); | |
$f = fopen("./guestbook.txt", "a") or die("Unable to write to file!"); | |
$line = "$author - $comment"; | |
fwrite($f, $line); | |
fclose($f); | |
} | |
function show_comments() { | |
// Read the file into an array of lines | |
$commentFile = file("guestbook.txt"); | |
foreach ($commentFile as $commentLine) { | |
// Split the line apart to get the author and comment | |
$ex = explode(" - ", $commentLine); | |
$author = $ex[0]; | |
$comment = implode(" - ", array_slice($ex, 1)); | |
echo "<p><span class='author'>$author</span><span class='comment'>$comment</span></p>"; | |
} | |
} | |
display_form(); | |
show_comments(); | |
?> | |
<style> | |
p { | |
border: 1px solid #333; | |
padding: 5px; | |
} | |
.author { | |
display: block; | |
font-weight: bold; | |
font-size: 0.8em; | |
} | |
.comment { | |
} | |
</style> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment