I created a PHP anti-spam contact form recently and I'd like to share it with people.
The concept is that automated bots usually fill in all fields on a form so that they don't get caught out by missing required fields. This form simply hides the email address using PHP and has an extra field that is shielded from users using CSS. If this extra hidden field is filled in, then the form aborts and throws up a warning message, whilst not submitting the form. (CSS is used as opposed to the hidden HTML attribute to prevent smarter bots from missing out this field - also, the field is called an inconspicuous name that bots would not understand). Unlike other forms, it doesn't inconvenience the user at all with CAPTCHA images or anti-spam questions.
It's a little basic, but I hope people will find it useful  The rest of the HTML page needs to be made (i.e. the header etc.) and the styling obviously needs to be done, but the PHP form is functional.
PHP Code:
<?php if($_POST['fred'] != "") { echo('<p style="color: #8B2323; font-size: 16px; font-weight: bold;">You may be using a text-only browser or you are a spambot. Sorry this has not been submitted</p>'); }
else { if(isset($_POST['name'])) {
if(($_POST['name'] == "") or ($_POST['email'] == "") or ($_POST['message'] == "")) { echo('<p style="color: #8B2323; font-size: 16px; font-weight: bold;">Please fill in all fields</p>'); }
else {
$name = $_POST['name']; $email = $_POST['email']; $company = $_POST['company']; $website = $_POST['website']; $formmessage = ($_POST['message']); $emailmessage = "You have received a submission from your contact form. Name: $name Email: $email Company: $company Website: $website Message: $formmessage ";
//Defining mail settings
$to = "Enter your email address here"; $subject = "Form submission sent"; $headers = "From: $email";
if(isset($name)) { mail($to,$subject,$emailmessage,$headers);
}
}
}
}
?> <?php if(isset($name)) { echo('<p style="color: #8B2323; font-size: 15px; font-weight: bold;">Your submission has been sent successfully</p>'); } ?> <form action="" method="post" id="contact"> <p><label><span>Your name: </span></label><input type="text" name="name" /><br /> <label><span>E-mail address: </span></label><input type="text" name="email" /><br /> <label><span>Company (optional): </span></label><input type="text" name="company" /><br /> <label><span>Web site address (optional): </span></label><input type="text" name="website" /><br /> <label><span>Your message: </span></label><textarea rows="15" cols="40" name="message"></textarea> <input type="text" id="fred" name="fred" style="visibility: hidden;"/> <input type="submit" value="Send" style="width: 8em; align: center;" /></p> </form>
Alternatively, download it from here
You can view a preview here.
Last edited by CSS4Life; 03-20-2008 at 12:23 PM..
|