What are common pitfalls to be aware of when implementing a PHP contact form on a website?

One common pitfall when implementing a PHP contact form is not properly sanitizing user input, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To mitigate this risk, always sanitize and validate user input before processing it in the form submission code.

// Sanitize user input before processing form submission
$name = htmlspecialchars($_POST['name']);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message = htmlspecialchars($_POST['message']);

// Validate email address
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Invalid email format";
    exit;
}

// Process form submission
// Your code to send the email or store the form data goes here