summaryrefslogtreecommitdiff
path: root/guestbook/guestbook.php
blob: 4f96a048c8d413e6e364a80e0c247eae16a6aa47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
// Function to sanitize user input
function sanitize_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

// Check if form has been submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // Get form data
  $name = sanitize_input($_POST["name"]);
  $message = sanitize_input($_POST["message"]);

  // Validate form data
  $errors = [];
  if (empty($name)) {
    $errors[] = "Name is required";
  }
  if (empty($message)) {
    $errors[] = "Message is required";
  }

  // If no errors, add entry to guestbook
  if (empty($errors)) {
    $entry = $name . "|" . $message . "\n";
    file_put_contents("entries.txt", $entry, FILE_APPEND | LOCK_EX);
  }
}

// Display guestbook entries
$entries = file("entries.txt");
?>

<!DOCTYPE html>
<html>
  <head>
    <title>Guestbook</title>
    <style>
      body {
        background-color: black;
        color: white;
        font-family: Arial, sans-serif;
        font-size: 16px;
      }
      form {
        display: flex;
        flex-direction: column;
        margin-bottom: 20px;
      }
      label {
        margin-bottom: 10px;
      }
      input, textarea {
        font-family: Arial, sans-serif;
        font-size: 16px;
        padding: 5px;
        margin-bottom: 10px;
        border-radius: 3px;
        border: none;
      }
      textarea {
        height: 100px;
      }
      input[type="submit"] {
        background-color: white;
        color: black;
        font-weight: bold;
        border-radius: 3px;
        border: none;
        padding: 10px;
        cursor: pointer;
      }
      .entry {
        margin-bottom: 20px;
      }
      .entry h3 {
        font-size: 18px;
        margin-bottom: 10px;
      }
      .entry p {
        font-size: 16px;
        margin-bottom: 10px;
      }
      .entry a {
        color: red;
        margin-left: 10px;
      }
    </style>
  </head>
  <body>
   <h1>Guestbook</h1>
    <?php
    // If there are errors, display them
    if (!empty($errors)) {
      echo "<ul>";
      foreach ($errors as $error) {
        echo "<li>" . $error . "</li>";
      }
      echo "</ul>";
    }
    ?>
    <form method="post">
      <label for="name">Name:</label>
      <input type="text" id="name" name="name">
  <label for="message">Message:</label>
  <textarea id="message" name="message"></textarea>
  <input type="submit" value="Submit">
</form>
<h2>Entries:</h2>
<?php
foreach ($entries as $entry) {
  list($name, $message) = explode("|", $entry);
  echo "<div class='entry'>";
  echo "<h3>" . htmlspecialchars($name) . "</h3>";
  echo "<p>" . htmlspecialchars($message) . "</p>";
  echo "</div>";
}
?>