Form with Captcha
Here is a link to a pdf file with all the instructions for a form with captcha.
Here it is so you can cut and paste someplace!
Form with Captcha
Three things...your contact.html
a matching captcha.php page and
your captcha_image.php page...
the first 2 you have to tweak to your requirements..fields etc.
captcha-image you just use as is.
Also, I have found that I have to revalidate the form every time...just because it is here, dreamweaver does something weird.
So I would remove the form validation and do it yourself any way you normally do.
I found this on a php page and I am ashamed to say I have lost the URL of the guy who wrote this. Who ever he is, I love him!
##########################################################
contact.html
###########################################################
<h2>Contact Form</h2>
Fields with a <strong>*</strong> are required.<br /><br />
<form action="captcha.php" method="post" onsubmit="MM_validateForm('name','','R','email','','R','captcha_input','','R');return document.MM_returnValue" >
<!-- form fields go here -->
<table width="400" border="1" cellspacing="0" cellpadding="2" bgcolor="#FFFFFF" align="center">
<tr>
<td width="100" class="tablecell">Name: <strong>*</strong> </td>
<td colspan="3" class="tablecell"><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td class="tablecell">Email:<strong>*</strong></td>
<td colspan="3" class="tablecell"><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td class="tablecell">Tel:</td>
<td colspan="3" class="tablecell"><input type="text" name="phone" /></td>
</tr>
<tr>
<td class="tablecell">Your Comment or Question:</td>
<td colspan="3" class="tablecell"> <textarea name=comment rows=5 cols=35></textarea></td>
</tr>
<tr>
<td colspan="2" align="center">Please copy the numbers on the left to the box on the right.<strong>*</strong></td>
</tr>
<tr>
<td><img src="captcha_image.php"/></td>
<td><input name="captcha_input" type="text" id="captcha_input" size="15"></td>
</tr>
<tr><td colspan="2" align="center"><input name="Submit" type="submit" class="submitbutton" id="Submit" value="Submit"></td></tr>
</table>
</form>
##########################################
captcha.php
###########################################
Note there are 2 places in this page that you have to tweak
This goes ABOVE the doctype...the very first thing on the page.
<?
session_start();
?>
in the body of the page...
<?
// *** The CAPTCHA comparison - http://frikk.tk ***
// *** further modifications - http://www.captcha.biz ***
// *** We need to make sure theyre coming from a posted form -
// If not, quit now ***
if ($_SERVER["REQUEST_METHOD"] <> "POST")
die("You can only reach this page by posting from the html form");
// *** The text input will come in through the variable $_POST["captcha_input"],
// while the correct answer is stored in the cookie $_COOKIE["pass"] ***
if ($_POST["captcha_input"] == $_SESSION["pass"])
{
// *** They passed the test! ***
// *** This is where you would post a comment to your database, etc ***
echo "Thank you very much for your inquiry. <br><br>";
echo "You will be contacted shortly<br><br>";
echo "This is what you sent <br><br>";
echo "Your Name: \"" . $_POST["name"] . "\" <br>";
echo "Your email: \"" . $_POST["email"] . "\" <br>";
echo "Your Telephone: \"" . $_POST["phone"] . "\" <br>";
echo "Your Comments: \"" . $_POST["comment"] . "\" <br>";
//sends email via php to the following address...change it
$mailuser = "you@emailprovider.net";
//echo 'default chosen address: '.$mailuser;
$header = "Return-Path: ".$mailuser."\r\n";
$header .= "From: Contact from Malahat Web Site <".$mailuser.">\r\n";
$header .= "Content-Type: text/html;";
$mail_body = '
Your Name: '. $_POST[name] . '<br>
Your email: '. $_POST[email] . '<br>
Your Telephone: '. $_POST[phone] . '<br>
Your Comments: '. $_POST[comment] . '<br>'
;
mail ($mailuser, 'From Contact Page', $mail_body, $header);
echo 'Thanks again!';
} else {
// *** The input text did not match the stored text, so they failed ***
// *** You would probably not want to include the correct answer, but
// unless someone is trying on purpose to circumvent you specifically,
// its not a big deal. If someone is trying to circumvent you, then
// you should probably use a more secure way besides storing the
// info in a cookie that always has the same name! :) ***
echo "Sorry, you did not enter the correct numbers into the form.<br><br>";
echo " - Please click the back button in your browser and try again <br><br>";
}
?>
########################################################
captcha_image.php
<?
// *** CAPTCHA image generation ***
// *** http://frikk.tk ***
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);
}
?>
|