123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- // Set the file path where the blog posts will be stored
- $file_path = 'posts.txt';
- // Set the page title
- $page_title = 'My PHP Blog';
- // Start the user session
- session_start();
- // Check if the user is logged in
- if (!isset($_SESSION['username'])) {
- // If not, check if the login form was submitted
- if (isset($_POST['login'])) {
- // Get the username and password from the form
- $username = trim($_POST['username']);
- $password = trim($_POST['password']);
- // Validate the input
- if ($username == 'admin' && $password == 'password') {
- // If the username and password are correct, set the session variable and redirect to the blog page
- $_SESSION['username'] = $username;
- header('Location: blog.php');
- exit;
- } else {
- // If the username and password are incorrect, display an error message
- $error = 'Incorrect username or password.';
- }
- }
- // Display the login page
- echo "<html><head><title>$page_title - Login</title></head><body>";
- echo "<h1>$page_title</h1>";
- if (isset($error)) { echo "<p>$error</p>"; }
- echo '<form method="post">';
- echo '<label>Username:</label><br>';
- echo '<input type="text" name="username"><br>';
- echo '<label>Password:</label><br>';
- echo '<input type="password" name="password"><br>';
- echo '<input type="submit" name="login" value="Login">';
- echo '</form>';
- echo '</body></html>';
- exit;
- }
- // Display the blog page
- echo "<html><head><title>$page_title</title></head><body>";
- echo "<h1>$page_title</h1>";
- // Check if the form was submitted
- if (isset($_POST['submit'])) {
- // Get the post title and content from the form
- $title = trim($_POST['title']);
- $content = trim($_POST['content']);
- // Validate the input
- if (strlen($title) == 0) {
- $error = 'Please enter a post title.';
- } else if (strlen($content) == 0) {
- $error = 'Please enter post content.';
- } else {
- // Format the post data as a string to be saved to the file
- $post = "=== $title ===\n$content\n";
- // Open the file in append mode and write the post data
- $fh = fopen($file_path, 'a');
- fwrite($fh, $post);
- fclose($fh);
- }
- }
- // Open the file and read the posts
- $fh = fopen($file_path, 'r');
- $posts = '';
- while (!feof($fh)) {
- // Read each line of the file and append it to the posts string
- $line = fgets($fh);
- $posts .= $line;
- }
- fclose($fh);
- // Display the blog posts on the page
- echo $posts;
- // Display the new post form
- echo '<h2>New Post</h2>';
- if (isset($error)) { echo "<p>$error</p>"; }
- echo '<form method="post">';
- echo '<label>Title:</label><br>';
- echo '<input type="text" name="title" value="';
- if (isset($title)) { echo htmlspecialchars($title); }
- echo '"><br>';
- echo '<label>Content:</label><br>';
- echo '<textarea name="content">';
- if (isset($content)) { echo htmlspecialchars($content); }
- echo '</textarea><br>';
- echo '<input type="submit" name="submit" value="Submit">';
- echo '</form>';
- // Display the HTML footer
- echo '</body></html>';
- ?>
|