Guestbook

I made a dead simple guestbook / notepad using php. It create a txt file, split it into an array at each EOL
$textArray = file($file, FILE_IGNORE_NEW_LINES);
and then display those lines in an unordered list
echo '<ul>';
foreach ($textArray as $index => $text) {
    if (!empty($text)) {
    echo '<li> '
    . htmlspecialchars($text) . 
    '</li>';
    }
}
echo '</ul>';
    
The form at the top let you append text to the File
<form action="guestbook.php" method="post">
    <input type="text" name="userText" placeholder="Enter text">
    <button type="submit" name="submit">Submit</button>
</form>

<!-- PHP code to handle form submission -->
<?php
if (isset($_POST['submit'])) {
    $text = $_POST['userText'] . PHP_EOL;

    file_put_contents($file, $text, FILE_APPEND);
    header("Location: guestbook.php"); // Redirect to refresh the page and display updated text
}
?>

- home -