26 Jul Captcha php coding question Posted by Liesl van Dalen | 6 Comments
I need to add a capthca to our online forms and I did the things as mentioned on the following site page: http://www.phpjabbers.com/captcha-image-verification-php19-5.html#comments

But i do not know where in my php code section to add the capthca php code section. When this form is working I will implement the changes to the Admissions contact us page. The relevant page I am currently working on is: http://www.aisj-jhb.com/Winter%20School/WinterSchoolContactUs.html

I also want to make the capthca a required field in the form as currently it actually send the form even if I did not add/inserted the capthca.

Thank you for your assistance in this matter I really do appreciate it.

Here is the relevant php code:

<?php

// get posted data into local variables
$siteaddress ="http://www.aisj-jhb.com";
$sitename = "AISJ";
$EmailTo = "lvdalen@aisj-jhb.com";
$Subject = "AISJ Winter School Registration Form";
$Student01 = Trim(stripslashes($_POST['Student01']));
$Student01_Grade = Trim(stripslashes($_POST['Student01_Grade']));
$Student01_Gender = Trim(stripslashes($_POST['Student01_Gender']));
$Student02 = Trim(stripslashes($_POST['Student02']));
$Student02_Grade = Trim(stripslashes($_POST['Student02_Grade']));
$Student02_Gender = Trim(stripslashes($_POST['Student02_Gender']));
$Student03 = Trim(stripslashes($_POST['Student03']));
$Student03_Grade = Trim(stripslashes($_POST['Student03_Grade']));
$Student03_Gender = Trim(stripslashes($_POST['Student03_Gender']));
$Student04 = Trim(stripslashes($_POST['Student04']));
$Student04_Grade = Trim(stripslashes($_POST['Student04_Grade']));
$Student04_Gender = Trim(stripslashes($_POST['Student04_Gender']));
$Parent1_Title = Trim(stripslashes($_POST['Parent1_Title']));
$First_Name = Trim(stripslashes($_POST['First_Name']));
$Last_Name = Trim(stripslashes($_POST['Last_Name']));
$Parent2_Title = Trim(stripslashes($_POST['Parent2_Title']));
$Parent2_FirstName = Trim(stripslashes($_POST['Parent2_FirstName']));
$Parent2_LastName = Trim(stripslashes($_POST['Parent2_LastName']));
$Current_Address = Trim(stripslashes($_POST['Current_Address']));
$Parent1_CellPhone = Trim(stripslashes($_POST['Parent1_CellPhone']));
$Parent2_CellPhone = Trim(stripslashes($_POST['Parent2_CellPhone']));
$Home_Phone = Trim(stripslashes($_POST['Home_Phone']));
$Email = Trim(stripslashes($_POST['Email']));
$DietaryNeeds = Trim(stripslashes($_POST['DietaryNeeds']));
$Bussing = Trim(stripslashes($_POST['Bussing']));
$Comments = Trim(stripslashes($_POST['Comments']));


// validation

$validationOK=true;


if (!$validationOK) {
print "<meta http-equiv="refresh" content="0;URL=WinterSchool_error.html">";
exit;
}



// prepare email body text
$Body = "";
$Body .= "AISJ-Requesting Information";
$Body .= "n";
$Body .= "--------------------------------------------------------";
$Body .= "n";
$Body .= "Student(s) Information";
$Body .= "n";
$Body .= "Student 01";
$Body .= "n";
$Body .= "Student Name and Last Name: ";
$Body .= "n";
$Body .= $Student01;
$Body .= "n";
$Body .= "n";
$Body .= "Current Grade: ";
$Body .= "n";
$Body .= $Student01_Grade;
$Body .= "n";
$Body .= "n";
$Body .= "Gender: ";
$Body .= "n";
$Body .= $Student01_Gender;
$Body .= "n";
$Body .= "n";

$Body .= "--------------------------------------------------------";
$Body .= "n";

$Body .= "Student 02";
$Body .= "n";
$Body .= "Student Name and Last Name: ";
$Body .= "n";
$Body .= $Student02;
$Body .= "n";
$Body .= "n";
$Body .= "Current Grade: ";
$Body .= "n";
$Body .= $Student02_Grade;
$Body .= "n";
$Body .= "n";
$Body .= "Gender: ";
$Body .= "n";
$Body .= $Student02_Gender;
$Body .= "n";
$Body .= "n";

$Body .= "--------------------------------------------------------";
$Body .= "n";

$Body .= "Student 03";
$Body .= "n";
$Body .= "Student Name and Last Name: ";
$Body .= "n";
$Body .= $Student03;
$Body .= "n";
$Body .= "n";
$Body .= "Current Grade: ";
$Body .= "n";
$Body .= $Student03_Grade;
$Body .= "n";
$Body .= "n";
$Body .= "Gender: ";
$Body .= "n";
$Body .= $Student03_Gender;
$Body .= "n";
$Body .= "n";

$Body .= "--------------------------------------------------------";
$Body .= "n";

$Body .= "Student 04";
$Body .= "n";
$Body .= "Student Name and Last Name: ";
$Body .= "n";
$Body .= $Student04;
$Body .= "n";
$Body .= "n";
$Body .= "Current Grade: ";
$Body .= "n";
$Body .= $Student04_Grade;
$Body .= "n";
$Body .= "n";
$Body .= "Gender: ";
$Body .= "n";
$Body .= $Student04_Gender;
$Body .= "n";
$Body .= "n";

$Body .= "--------------------------------------------------------";
$Body .= "n";

$Body .= "Parent Guardian Information";
$Body .= "n";
$Body .= "Parent1_Title: ";
$Body .= "n";
$Body .= $Parent1_Title;
$Body .= "n";
$Body .= "n";
$Body .= "Parent1 First Name: ";
$Body .= "n";
$Body .= $First_Name;
$Body .= "n";
$Body .= "n";
$Body .= "Parent 1 Last Name: ";
$Body .= "n";
$Body .= $Last_Name;
$Body .= "n";
$Body .= "n";
$Body .= "Parent 2 Title: ";
$Body .= "n";
$Body .= $Parent2_Title;
$Body .= "n";
$Body .= "n";
$Body .= "Parent 2 First Name: ";
$Body .= "n";
$Body .= $Parent2_FirstName;
$Body .= "n";
$Body .= "n";
$Body .= "Parent 2 Last Name: ";
$Body .= "n";
$Body .= $Parent2_LastName;
$Body .= "n";
$Body .= "n";

$Body .= "--------------------------------------------------------";
$Body .= "n";

$Body .= "Current Address: ";
$Body .= "n";
$Body .= $Current_Address;
$Body .= "n";
$Body .= "n";
$Body .= "Parent 1 CellPhone: ";
$Body .= "n";
$Body .= $Parent1_CellPhone;
$Body .= "n";
$Body .= "n";
$Body .= "Parent 2 CellPhone: ";
$Body .= "n";
$Body .= $Parent2_CellPhone;
$Body .= "n";
$Body .= "n";
$Body .= "Email: ";
$Body .= "n";
$Body .= $Email;
$Body .= "n";
$Body .= "n";
$Body .= "Home Phone: ";
$Body .= "n";
$Body .= $Home_Phone;
$Body .= "n";
$Body .= "n";
$Body .= "Indicate any special dietary needs: ";
$Body .= "n";
$Body .= $DietaryNeeds;
$Body .= "n";
$Body .= "n";
$Body .= "Do you require bussing?: ";
$Body .= "n";
$Body .= $Bussing;
$Body .= "n";
$Body .= "n";
$Body .= "Comments or Questions: ";
$Body .= "n";
$Body .= $Comments;
$Body .= "n";
$Body .= "n";


print $EmailTo;
print $Subject;
print $Body;
$emailFromToUse = "From: ".$EmailFrom;
print $emailFromToUse;

$headers = $emailFromToUse."rn";

// send email
//$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
$success = mail($EmailTo, $Subject, $Body, $headers);



// redirect to success page
if ($success){
//print("OK");
print "<meta http-equiv="refresh" content="0;URL=WinterSchool_ok.html">";
}
else{
//print("FAIL");
print "<meta http-equiv="refresh" content="0;URL=WinterSchool_error.html">";
}
/**/
//This sends a confirmation to your visitor
mail ("$Email","Thank You for visiting $sitename",
"Hi $First_Name,n
Thank you for your interest in $sitename!n
Your query has been submitted and we will contact you shortly!n
Kind Regards,
$sitename
$siteaddress","From:$sitename") ;

?>

6 Replies to "Captcha php coding question"

Sasho Valkanov July 30, 2012 at 2:43 pm | Reply

+1

The IF statement which checks if the captcha is correctly entered or not should be placed before

$success = mail($EmailTo, $Subject, $Body, $headers);

So that line will only execute if the captcha is correct.
Liesl van Dalen July 30, 2012 at 3:11 pm

0

Hi Mark,

I did what you said in your comment and I testing the form by deliberately entering the incorrect captcha. Yet the form still submitted the information. So there is obviously still a problem with my code somewhere.

This is the new php code after incorporating your comment:
<?php
.....

$headers = $emailFromToUse."rn";

// send email
//$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
session_start();
if ($_POST["vercode"] != $_SESSION["vercode"] OR $_SESSION["vercode"]=='') {
echo '<strong>Incorrect verification code.</strong>';
} else {
// add form data processing code here
echo '<strong>Verification successful.</strong>';
};

$success = mail($EmailTo, $Subject, $Body, $headers);

// redirect to success page
if ($success){
//print("OK");
print "<meta http-equiv="refresh" content="0;URL=WinterSchool_ok.html">";
}
else{
//print("FAIL");
print "<meta http-equiv="refresh" content="0;URL=WinterSchool_error.html">";
}
/**/
//This sends a confirmation to your visitor
mail ("$Email","Thank You for visiting $sitename",
"Hi $First_Name,n
Thank you for your interest in $sitename!n
Your query has been submitted and we will contact you shortly!n
Kind Regards,
$sitename
$siteaddress","From:$sitename") ;
Liesl van Dalen August 7, 2012 at 8:35 am

0

My Captcha code is still not working. what can I still do to ensure that it will work?
Sasho Valkanov August 7, 2012 at 3:21 pm

0

line 16 to 35 should go where line 13 is.
Sasho Valkanov August 7, 2012 at 3:22 pm

0

line 16 to 35 should go where line 13 is.
Liesl van Dalen August 9, 2012 at 2:44 pm | Reply

0

Thank you Mark, my form works now :)
Just one more question: how can I make my captcha be a mixture of numbers and alphabet letters?

I know that in order to change that I need to work in the captcha php file.

Thank you for your assistance in this.
  • 1


Please be polite and helpful and do not spam or offend others. We promise you will be treated the same way.

Log in your free account or if you still haven't joined our Webmaster Community Reward Program, you can create your free account now.

Posting tip:
if you use code in your comments please put it in these tags [php], [sql], [css], [js]
PHP code example: [php] echo date("Y-m-d"); [/php]

Thank you,
~ PHPJabbers team ~