*/}}

chatgpt_test_2.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // Set the file path where the blog posts will be stored
  3. $file_path = 'posts.txt';
  4. // Set the page title
  5. $page_title = 'My PHP Blog';
  6. // Start the user session
  7. session_start();
  8. // Check if the user is logged in
  9. if (!isset($_SESSION['username'])) {
  10. // If not, check if the login form was submitted
  11. if (isset($_POST['login'])) {
  12. // Get the username and password from the form
  13. $username = trim($_POST['username']);
  14. $password = trim($_POST['password']);
  15. // Validate the input
  16. if ($username == 'admin' && $password == 'password') {
  17. // If the username and password are correct, set the session variable and redirect to the blog page
  18. $_SESSION['username'] = $username;
  19. header('Location: blog.php');
  20. exit;
  21. } else {
  22. // If the username and password are incorrect, display an error message
  23. $error = 'Incorrect username or password.';
  24. }
  25. }
  26. // Display the login page
  27. echo "<html><head><title>$page_title - Login</title></head><body>";
  28. echo "<h1>$page_title</h1>";
  29. if (isset($error)) { echo "<p>$error</p>"; }
  30. echo '<form method="post">';
  31. echo '<label>Username:</label><br>';
  32. echo '<input type="text" name="username"><br>';
  33. echo '<label>Password:</label><br>';
  34. echo '<input type="password" name="password"><br>';
  35. echo '<input type="submit" name="login" value="Login">';
  36. echo '</form>';
  37. echo '</body></html>';
  38. exit;
  39. }
  40. // Display the blog page
  41. echo "<html><head><title>$page_title</title></head><body>";
  42. echo "<h1>$page_title</h1>";
  43. // Check if the form was submitted
  44. if (isset($_POST['submit'])) {
  45. // Get the post title and content from the form
  46. $title = trim($_POST['title']);
  47. $content = trim($_POST['content']);
  48. // Validate the input
  49. if (strlen($title) == 0) {
  50. $error = 'Please enter a post title.';
  51. } else if (strlen($content) == 0) {
  52. $error = 'Please enter post content.';
  53. } else {
  54. // Format the post data as a string to be saved to the file
  55. $post = "=== $title ===\n$content\n";
  56. // Open the file in append mode and write the post data
  57. $fh = fopen($file_path, 'a');
  58. fwrite($fh, $post);
  59. fclose($fh);
  60. }
  61. }
  62. // Open the file and read the posts
  63. $fh = fopen($file_path, 'r');
  64. $posts = '';
  65. while (!feof($fh)) {
  66. // Read each line of the file and append it to the posts string
  67. $line = fgets($fh);
  68. $posts .= $line;
  69. }
  70. fclose($fh);
  71. // Display the blog posts on the page
  72. echo $posts;
  73. // Display the new post form
  74. echo '<h2>New Post</h2>';
  75. if (isset($error)) { echo "<p>$error</p>"; }
  76. echo '<form method="post">';
  77. echo '<label>Title:</label><br>';
  78. echo '<input type="text" name="title" value="';
  79. if (isset($title)) { echo htmlspecialchars($title); }
  80. echo '"><br>';
  81. echo '<label>Content:</label><br>';
  82. echo '<textarea name="content">';
  83. if (isset($content)) { echo htmlspecialchars($content); }
  84. echo '</textarea><br>';
  85. echo '<input type="submit" name="submit" value="Submit">';
  86. echo '</form>';
  87. // Display the HTML footer
  88. echo '</body></html>';
  89. ?>