Here's a freebie from me. Its a quick one, so apologies if there is an error or whatever.
1. The form page (contact.html or whatever)
Code:
<P><STRONG>Contact us!</STRONG></P>
<P>ALL fields are required.</P>
<form method="POST" action="contact.php">
Name: <input name="name" size="23"><br />
E-mail: <input name="email" size="23"><br />
Subject: <input name="subject" size="23"><br />
Message:
<textarea cols="70" rows="7" name="message"></textarea><br />
<br>
<img src="captcha_image.php" width="100" height="20"/>:<input name="captcha" size="14"><br>
<input type="submit" value="Submit"></form>
</P></BODY></HTML>
2. contact.php (the form processor)
Code:
<?php
session_start();
if ($_POST["captcha"] != $_SESSION["pass"])
{
die('CAPTCHA Failed, press the Back button on your browser and try again.');
}
$to = "bleh@google.com"; //edit this line to YOUR email
// do not edit below this line!
$from = $_POST["email"];
$subject = $_POST["subject"];
$name = $_POST["name"];
$message = $_POST["message"];
$messagesent = "New contact form submission:
\r\n---\r\n
Name: $name \r\n
Email: $from \r\n
Message: \r\n $message
\r\n\r\n
Sender's IP: $HTTP_SERVER_VARS[REMOTE_ADDR]";
$headers = "From: $from";
if (!preg_match("/^([a-zA-Z -])+$/",$name))
{
die('Name must consist of letters only.');
}
if (!preg_match("/^([0-9a-zA-Z\.\,\;\?\!\'\ \-])+$/",$subject))
{
die('Subject can only contain letters, numbers, and punctuation.');
}
if (!preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$from))
{
die('Invalid e-mail address.');
}
if (!preg_match("/^([0-9a-zA-Z\.\,\;\?\!\'\ \-])+$/",$message))
{
die('Message can only contain letters, numbers, and punctuation.');
}
mail($to,$subject,$messagesent,$headers);
echo "Form has been submitted successfully. Thank you!";
?>
3. captcha_image.php (the captcha)
Code:
<?
// CAPTCHA image generation
// copyright (c) discussionscript.com
session_start();
// *** Tell the browser what kind of file is come'n at 'em! ***
header("Content-Type: image/jpeg");
// *** Send a generated image to the browser ***
die(create_image());
// *** Function List ***
function create_image()
{
// *** Generate a passcode using md5
// (it will be all lowercase hex letters and numbers ***
$md5 = md5(rand(0,9999));
$pass = substr($md5, 10, 5);
// *** Set the session cookie so we know what the passcode is ***
$_SESSION["pass"] = $pass;
// *** Create the image resource ***
$image = ImageCreatetruecolor(100, 20);
// *** We are making two colors, white and black ***
$clr_white = ImageColorAllocate($image, 255, 255, 255);
$clr_black = ImageColorAllocate($image, 0, 0, 0);
// *** Make the background black ***
imagefill($image, 0, 0, $clr_black);
// *** Set the image height and width ***
imagefontheight(15);
imagefontwidth(15);
// *** Add the passcode in white to the image ***
imagestring($image, 5, 30, 3, $pass, $clr_white);
// *** Throw in some lines to trick those cheeky bots! ***
imageline($image, 5, 1, 50, 20, $clr_white);
imageline($image, 60, 1, 96, 20, $clr_white);
// *** Return the newly created image in jpeg format ***
return imagejpeg($image);
// *** Just in case... ***
imagedestroy($image);
}
?>