Jump to content

add captcha to contact form


fullyloaded

Recommended Posts

hi

i was wondering if anyone had any idea how i can add code A  to code b below? code a is my captcha code and code b is my contact form code thanks..

 

CODE A CAPTCHA:

<?php
if(isset($_POST["captcha"]))
if($_SESSION["captcha"]==$_POST["captcha"])
{
echo 'CAPTHCA is valid; proceed the message';
}
else
{
echo 'CAPTHCA is not valid; ignore submission';
}
?>

CODE B CONTACT FORM:

<?
$mailto = "me@blah.com";
$cc = "";
$bcc = "";
$subject = "Email subject";
$vname = "blah blah";
$email = $_POST['email'];
function validateEmail($email)
{
if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email))
return true;
else
return false;
}
if(strlen($_POST['name']) < 1  || strlen($_POST['message']) < 1 || validateEmail($email) == FALSE)
{
$emailerror = 'Error:';
if(empty($_POST['name']))
{
$emailerror .= '<li>Enter name</li>';
}
if(validateEmail($email) == FALSE)
{
$emailerror .= '<li>Enter valid email</li>';
}
if(empty($_POST['message']))
{
$emailerror .= '<li>Enter message</li>';
}
}
else
{
$emailerror .= "Your email has been sent successfully";
$timestamp = date("F j, Y, g:ia");
$messageproper ="\n\n" .
"Name: " .
ucwords($_POST['name']) .
"\n" .
"Email: " .
ucwords($email) .
"\n" .
"Comments: " .
$_POST['message'] .
"\n" .
"\n\n" ;
$messageproper = trim(stripslashes($messageproper));
mail($mailto, $subject, $messageproper, "From: \"$vname\" <".$_POST['e_mail'].">\nReply-To: \"".ucwords($_POST['first_name'])."\" <".$_POST['e_mail'].">\nX-Mailer: PHP/" . phpversion() );
}
?>

Link to comment
https://forums.phpfreaks.com/topic/254866-add-captcha-to-contact-form/
Share on other sites

put all the email sending code within the if where "echo 'CAPTHCA is valid; proceed the message';" is.

then change 'echo 'CAPTHCA is not valid; ignore submission';" to a prettier error message for the user to view.

 

Either that or have a look at google's reCAPTCHA

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.