Email form with Captcha help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
apg1985
Forum Newbie
Posts: 4
Joined: Mon Aug 11, 2008 11:14 am

Email form with Captcha help please

Post by apg1985 »

Hi Guys,

new to php so have a basic understanding.

I've got the email sending and a redirect to the homepage. But I want a captcha for security. Ive got the image for the captcha displaying but not sure how I would make it work.

html captcha:

<img src="aaaacaptcha.php" style="display: block;margin-bottom: 5px;"/>
<input type="text" name="captcha2" id="captcha2" value="captcha2" />

code for the captcha image:
<?php
session_start();

$RandomStr = md5(microtime());

$ResultStr = substr($RandomStr,0,5);

$NewImage =imagecreatefromjpeg("img.jpg");

$LineColor = imagecolorallocate($NewImage,233,239,239);
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);

imageline($NewImage,1,1,40,40,$LineColor);
imageline($NewImage,1,100,60,0,$LineColor);

imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);

$_SESSION['captcha'] = $ResultStr;

header("Content-type: image/jpeg");

imagejpeg($NewImage);

?>

code for the email php:

<?php
if(isset($_POST['submit'])) {
$to = "my email address";
$subject = "web form";
$firstname = $_POST['first name'];
$lastname = $_POST['last name'];

$body = " email fields";

header("Location: homepage address");
mail($to, $subject, $body);
}
else
{
echo "Not Sent";
}
?>

Please help :)
Post Reply