Last active
November 16, 2024 11:47
-
-
Save andrewlimaza/958826feac907114a57462bfc8d535ff to your computer and use it in GitHub Desktop.
Simple honeypot for an HTML form using PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//check if form was sent | |
if($_POST){ | |
$to = '[email protected]'; | |
$subject = 'Testing HoneyPot'; | |
$header = "From: $name <$name>"; | |
$name = $_POST['name']; | |
$email = $_POST['email']; | |
$message = $_POST['message']; | |
//honey pot field | |
$honeypot = $_POST['firstname']; | |
//check if the honeypot field is filled out. If not, send a mail. | |
if( ! empty( $honeypot ) ){ | |
return; //you may add code here to echo an error etc. | |
}else{ | |
mail( $to, $subject, $message, $header ); | |
} | |
} | |
?> | |
<html> | |
<head> | |
<title>HoneyPot for HTML Form Example</title> | |
<style> | |
.hide-robot{ | |
display:none; | |
} | |
</style> | |
</head> | |
<body> | |
<form method="post" action="#my-form" id="my-form"> | |
<!-- Create fields for the honeypot --> | |
<input name="firstname" type="text" id="firstname" class="hide-robot"> | |
<!-- honeypot fields end --> | |
<input name="name" type="text" id="name" placeholder="Name" required><br> | |
<input name="email" type="email" id="email" placeholder="Email" required><br> | |
<textarea name="message" id="message" placeholder="Enter your message here" required></textarea><br> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> |
These are good tips. Can anyone suggest if its a good idea to use a form backend service to stop form spam?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello all,
I am also struggling with spam and looking for a way to get this problem under control.
I have a question about the code, is this exactly as specified above inserted on the same page as text or is this inserted under Contact form 7 (additional settings).
do I need to customize the code other than the placeholder texts?
Sorry for my simple questions.
Thanks in advance :)