What are some alternative methods to using reCAPTCHA for spam prevention in PHP contact forms?
Using reCAPTCHA for spam prevention in PHP contact forms can be effective, but some users may find it intrusive or difficult to use. An alternative method is to implement a honeypot technique, where a hidden form field is added to the contact form that only bots will fill out. If the hidden field is filled, the submission can be flagged as spam.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Honeypot field
if (!empty($_POST['website'])) {
// This is likely a bot submission, handle accordingly
echo "Spam submission detected.";
exit;
}
// Process the form submission
// Add code here to send the email or store the form data
}
?>
<form method="post" action="">
<input type="text" name="name" placeholder="Name" required><br>
<input type="email" name="email" placeholder="Email" required><br>
<textarea name="message" placeholder="Message" required></textarea><br>
<!-- Honeypot field -->
<input type="text" name="website" style="display: none;">
<input type="submit" value="Submit">
</form>