What are some best practices for designing a guestbook form in PHP?

When designing a guestbook form in PHP, it is important to validate user input to prevent any malicious code injection. Use PHP functions like htmlspecialchars() to sanitize user input and protect against cross-site scripting attacks. Additionally, consider implementing a CAPTCHA to prevent spam submissions and ensure the integrity of the guestbook entries.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars($_POST['name']);
    $message = htmlspecialchars($_POST['message']);
    
    // Validate input
    if (!empty($name) && !empty($message)) {
        // Save the guestbook entry to a file or database
        // Redirect to a success page
        header("Location: success.php");
        exit();
    } else {
        // Display an error message
        echo "Please fill out all fields.";
    }
}
?>