123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- // Set the filename for the blog data
- $filename = "blog.txt";
- // Check if the file exists and create it if it doesn't
- if (!file_exists($filename)) {
- $file = fopen($filename, "w");
- fclose($file);
- }
- // Read the current blog data from the file
- $data = file_get_contents($filename);
- // If a new post was submitted, append it to the current blog data
- if (isset($_POST['new_post'])) {
- // Format the current time and date
- $timestamp = date("m/d/Y H:i:s");
- // Convert newline characters to <br> elements and remove the newlines
- $post = str_replace("\n", "", nl2br($_POST['new_post']));
- // Append the timestamp and the post content to the data
- $data .= "$timestamp: $post\n";
- file_put_contents($filename, $data);
- }
- // Set the CSS styles inline on the body element
- echo "<body style='margin: 0 auto; width: 80%; text-align: center;'>";
- // Display the blog posts in a list
- echo "<ul>";
- foreach (explode("\n", $data) as $post) {
- echo "<li>$post</li>";
- }
- echo "</ul>";
- // Display a form for creating new posts
- echo '<form action="" method="post">';
- echo '<textarea name="new_post" rows="5" cols="50"></textarea><br>';
- echo '<input type="submit" value="Submit">';
- echo '</form>';
- // Close the body element
- echo "</body>";
|