I have a contact form on my company’s website that I have had serious problems with in the past… The past being before I implemented Akismet into the code.

Akismet is a service provided by Automattic (the creators of WordPress) and is an excellent API based spam blocking system. I’ve been using it for quite a while on my blog and rogue spams hardly EVER make it into my comments. Probably somewhere in the neighborhood of only 1-2 out of thousands make it through.

I will post both the basic code for a contact form as well as the Akismet PHP5 Class (from Alex) and the include file I created to utilize the PHP5 Class.

Here is the basic form (named contact.php), with the PHP code on top, that I use.

<?php
if ($_SERVER[“REQUEST_METHOD”] == “POST”)
{
include ‘akismet.php’;

$to = “YOUR EMAIL ADDRESS“;
$subject = $_REQUEST[“Subject”];
$message = $_REQUEST[“Message”].”<BR><BR>”.$_REQUEST[“Phone”].”<BR>”.$_REQUEST[“URL”];
$from = $_REQUEST[“Email”];

$headers = ‘From:’.$from.”rn” .
‘X-Mailer: PHP/’ . phpversion() .”rn”.
‘Content-type: text/html’.”rn”.
‘Date: ‘.date(”r”).”rn”;

mail($to, $subject, $message, $headers);

echo “Message Sent!”;
die();
}
?>
<html>
<head>
<title>My Contact Form</title>
</head>
<body>

<form action=”contact.php” method=”POST”>
<table align=”left”>
<tr>
<td align=”right”>Your name:</td>
<td><input name=”Name” type=”text”></td>
</tr>
<tr>
<td align=”right”>Website:</td>
<td><input name=”URL” type=”text”></td>
</tr>
<tr>
<td align=”right”>Your email:</td>
<td><input name=”Email” type=”text”></td>
</tr>
<tr>
<td align=”right”>Subject:</td>
<td><input name=”Subject” type=”text”></td>
</tr>
<tr>
<td align=”right”>Message:</td>
<td><textarea name=”Message” style=”width: 224px; height: 83px”></textarea></td>
</tr>
<tr>
<td align=”right”>Your phone:</td>
<td><input name=”Phone” type=”text”>(Optional)</td>
</tr>

<tr>
<td> </td>
<td><input name=”Submit” type=”submit” value=”submit”></td>
</tr>
</table>
</form>

</body>
</html>

contact.php includes the code I created to use the Akismet PHP5 Class mentioned earlier.

Here is the code for the akismet.php. Be sure to change the $WebsiteURL variable to your website address and $APIKey variable to the API key you already have. If you do not have one, signup for a WordPress.com account and you can get one free.

<?php
//Akisment PHP5 Class from Alex (http://www.achingbrain.net/stuff/php/akismet)
include ‘Akismet.class.php’;

//ENTER YOUR API KEY ON THE LINE BELOW!
$APIKey = ‘YOUR_API_KEY‘;

//Enter your web site address below. This is used as a parameter in the API call to Akismet…
$WebsiteURL = ‘YOUR_WEBSITE_ADDRESS‘;

//Call to check for valid API Key…
$akismet = new Akismet($WebsiteURL, $APIKey);

//Check to see if your API key is valid, if not, it will tell you so and stop.

if($akismet->isKeyValid()) {}else{echo “Your API key is NOT valid!”;die();}

if ($_SERVER[“REQUEST_METHOD”] == “POST”) {

//CHANGE the $_REQUEST items to match your form field input element names

$akismet = new Akismet($WebsiteURL, $APIKey); //
$akismet->setCommentAuthor($_REQUEST[“Name”]);
$akismet->setCommentAuthorEmail($_REQUEST[“Email”]);
$akismet->setCommentAuthorURL($_REQUEST[“URL”]);
$akismet->setCommentContent($_REQUEST[“Message”]);
$akismet->setPermalink($_SERVER[“HTTP_REFERER”]);

//IF THE MESSAGE IS FOUND TO BE SPAM, A MESSAGE WILL BE DISPLAYED. Customize as needed.

if($akismet->isCommentSpam()) {
echo “For some reason, your message was flagged as a possible SPAM message.”;
die();
}
//IF THE MESSAGE MAKES IT TO THIS POINT, IT IS VALID AND THE SCRIPT THAT CALLED THIS
//FILE WILL CONTINUE PROCESSING THE INPUT ACCORDINGLY…

}
?>

Click here to download a ZIP of the three files used in this example.

If you have any questions, leave a comment here and I’ll do my best to answer in a timely manner…