|
Answer
by: Stuart Pierce, Aplus.Net Knowledge Base Support
You
can see below complete source code examples of an online
form
which will ask the user to input a validation code upon submission.
This validation code will prevent automatic internet bots from spamming
your online form with unappropriate entries. If you use the files below
without any modification, your form will look like this:
As
you can see, the user has to enter four digits into the respective
text field. If they are not correct, the form message will not be sent
to the recipient email address. The implementation of this online form
consists of three different files: the HTML form and two PHP scripts.
All three of them should be uploaded to the same web directory.
Note:
This formmail application is supported on our Unix-based hosting
plans only.
Important:
In order to use this PHP formmail, it is required that you
enable the GD2 module in PHP. Here is how to achieve this:
Note:
The coding examples below might get corrupted, if you use a
regular copy/paste in order to create your own form. To avoid this,
please click the link below to download a ZIP archive of the three form
files:
I.
The form file - formmail.php.
This file is the actual location of your form. If you upload to your
main html
directory, the form will be available at
http://yourdomain.com/formmail.php, where "yourdomain.com" is your
actual hosted domain name.
<?php
session_start();
$_SESSION['random_number']=rand(1000, 9999);
?>
<html>
<head>
<title>Formmail</title>
</head>
<body>
<!-------------begin form------------>
<FORM ACTION="mail.php" METHOD="POST"
NAME="contact_form">
<TABLE>
<TR>
<TD><font size="2" face="Verdana,
Arial, Helvetica,
sans-serif">Name:</font></TD>
<TD><input type=text
name="contact_name"></TD>
</TR>
<TR>
<TD><font size="2" face="Verdana,
Arial, Helvetica, sans-serif">Email
Address:</font></TD>
<TD><input type=text
name="contact_email"></TD>
</TR>
<TR>
<TD><font size="2" face="Verdana,
Arial, Helvetica,
sans-serif">Comments:</font></TD>
<TD><textarea name="comment" cols="40"
rows="3"></textarea></TD>
</TR>
<tr>
<td>
<font size="2" face="Verdana, Arial, Helvetica,
sans-serif">Please enter the digits you see below:
</font>
</td>
<TD><input type=text
name="val_code"></TD>
</tr>
<tr>
<td><img
src="http://yourdomain.com/image_number1.php?n=<?=rand(1000,
9999)?>" alt="number" /> <TR> //Please replace with your hosted
domain name
<TD><input type="reset" value="Reset"
name="Reset"></TD>
<TD><input type="submit" value="Submit"
name="Submit"></TD>
</TR>
</TABLE>
II.
PHP formmail script - mail.php.
Please pay attention to the text in bold, where you have to
replace you@yourdomain.com
with your email address. Do not remove the quotes.
<?
session_start();
function checkOK($field)
{
if (eregi("\r",$field)){
die("Invalid Input!");
}
}
$val_code=$_POST['val_code'];
$name=$_POST['contact_name'];
checkOK($name);
$email=$_POST['contact_email'];
checkOK($email);
$comments=$_POST['comment'];
checkOK($comments);
$to="you@yourdomain.com";
//Replace with an email
address where the form results will be sent
if ("{$_SESSION['random_number']}"!="{$val_code}") {
echo "Wrong Validation Code. Try again.";
}
else {
$message="$name just filled in your comments form. They
said:\n$comments\n\nTheir e-mail address was: $email\n;
if(mail($to,"Comments From Your Site",$message, "From: $email")) {
echo "Thanks for your comments.";
} else {
echo "There was a problem sending the mail. Please check that you
filled in the form correctly.";
}
}
?>
III.
PHP script for the image with the randomly generated digits - image_number.php.
<?php
session_start();
header('Pragma: no-cache');
header('Expires: -1');
header('Cache-control: no-cache');
$number=$_SESSION['random_number'];
//$number=123;
header ("Content-type: image/png");
$im = @imagecreatetruecolor(60, 19)
or die("Cannot Initialize new
GD image stream");
$text_color = imagecolorallocate($im, 199, 224, 212);
$text_color1 = imagecolorallocate($im, 0, 0, 0);
$background = imagecolorallocate($im, 121, 179, 11);
$e=imagefilledrectangle ( $im, 0, 0,100, 50, $background);
imagestring($im, 8, 2, 2, $number, $text_color);
imagestring($im, 8, 4, 4, $number, $text_color1);
imagegif($im);
imagedestroy($im);
?>
Note:
The coding examples above are considered secure. However, we are
not responsible for any security issues they might lead to.
|