Jump to content

Email form with Captcha help please


apg1985

Recommended Posts

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  :)

Link to comment
https://forums.phpfreaks.com/topic/140826-email-form-with-captcha-help-please/
Share on other sites

<?php
session_start();
if(isset($_POST['submit'])) {
  if(empty($_POST['captcha2']) || $_SESSION['captcha'] != $_POST['captcha2'] ){
    die("Invalid captcha");
  }

$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";
}
?>

Brilliant that worked, thanks for that.

 

At the moment if its correct it goes through to the homepage which is great, but ive got a error page as well error.php.

 

How would I get it to go to error.php if it was incorrect??

 

would it be

 

if(isset($_POST['submit'])) {

  if(empty($_POST['captcha2']) || $_SESSION['captcha'] != $_POST['captcha2'] ){

    die("Location: error.php");

  }

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.