-
-
Save dbstraight/9acd848d661ac2a4f81f to your computer and use it in GitHub Desktop.
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> | |
<head> | |
<meta charset="utf-8"> | |
<title>Blog</title> | |
</head> | |
<body> | |
<form action="index.php" method="post"> | |
<input type="text" name="title"><p> | |
<textarea name="body"></textarea><p> | |
<input type="submit"> | |
</form> | |
<?php | |
// The directory where we will store posts | |
$posts_dir = dirname(__FILE__) . '/posts/'; | |
// If title and body are given, we need to add the new post | |
if ($_POST['title'] && $_POST['body']) { | |
// Make sure the person submitting the post isn't trying to | |
// do anything malicious by "escaping" all characters with | |
// special meaning in HTML. | |
$title = htmlspecialchars($_POST['title']); | |
$body = htmlspecialchars($_POST['body']); | |
// Write the post to a file named with a timestamp. The | |
// content consists of the title on the first line and the | |
// body thereafter. | |
file_put_contents($posts_dir . date('U'), $title . '\n' . $body); | |
} | |
// Show content of all posts in reverse chronological order | |
foreach (scandir($posts_dir, SCANDIR_SORT_DESCENDING) as $post_file) { | |
$full_path = $posts_dir . '/' . $post_file; | |
// Read in file | |
$content = file_get_contents($full_path); | |
// Split by newline character, first item will be title, | |
// second item will be body. | |
$items = explode('\n', $content, 2); | |
$title = $items[0]; | |
$body = $items[1]; | |
// Write out the post | |
echo '<h1>' . $title . '</h1>'; | |
echo $body; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment