...

View Full Version : Add recaptcha to existing form validation



jewelsmac66
02-20-2010, 04:02 PM
Hello all, I have signed up for the recaptcha keys and code but am having problems getting the recaptcha validation to work with my existing form's validation process. How do I add the recaptcha validation into the form's exisiting validation process? This used to work fine and would send the form and file attachments directly to my inbox, before I started playing with the recaptcha code. Thank you in advance!

I have tried using the followng combinations with no luck:
and
AND
. (space, period, space)
if (sizeof($_POST) > 1 . !$resp->is_valid) {
if (sizeof($_POST) > 1); (!$resp->is_valid); {


This is the error I am getting with the PHP code below:
Parse error: syntax error, unexpected T_LOGICAL_AND in /home/content/j/e/w/jewelsmac6/html/formhandler2.php on line 213

PHP code:


<?php

if ($_SERVER['REQUEST_METHOD']=="POST"){

require_once('recaptchalib.php');
$publickey = "xxxxxxxx";
$privatekey = "xxxxxxx";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);



// we'll begin by assigning the To address and message subject

$to="jmacdesigns@gmail.com";

$subject="E-mail with attachment";



// get the sender's name and email address

// we'll just plug them a variable to be used later

$from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">";



// generate a random string to be used as the boundary marker

$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";



// now we'll build the message headers

$headers = "From: $from\r\n" .

"MIME-Version: 1.0\r\n" .

"Content-Type: multipart/mixed;\r\n" .

" boundary=\"{$mime_boundary}\"";



// here, we'll start the message body. all entries ARE case sensitive!!

// this is the text that will be displayed

// in the e-mail

$message = $_POST['message'];



$YourName = $_POST['YourName'];

$BizName = $_POST['BizName'];

$address = $_POST['address'];

$city = $_POST['city'];

$State = $_POST['State'];

$zipcode = $_POST['zipcode'];

$email = $_POST['email'];

$JobType = $_POST['JobType'];

$JobDimensions = $_POST['JobDimensions'];

$Quantity = $_POST['Quantity'];

$PaperWhite = $_POST['PaperWhite'];

$PaperOffWhite = $_POST['PaperOffWhite'];

$PaperCSWhite = $_POST['PaperCSWhite'];

$PaperCSOffWhite = $_POST['PaperCSOffWhite'];

$PaperOther = $_POST['PaperOther'];

$LaminationYes = $_POST['LaminationYes'];

$LaminationNo = $_POST['LaminationNo'];

$DateReq = $_POST['DateReq'];

$comments = $_POST['comments'];



$message= "Your Name: $YourName\n\nBusiness Name: $BizName\n\nAddress: $Address\n\nCity: $City\n\nState: $State\n\nZip Code: $ZipCode\n\nEmail: $email\n\nJob Type: $JobType\n\nJob Dimensions: $JobDimensions\n\nQuantity: $Quantity\n\nPaper White: $PaperWhite\n\nPaper Off White: $PaperOffWhite\n\nPaper Card Stock White: $PaperCSWhite\n\nPaper Card Stock Off White: $PaperCSOffWhite\n\nPaper Other: $PaperOther\n\nLamination Yes: $LaminationYes\n\nLamination No: $LaminationNo\n\nDate Required: $DateReq\n\nComments: $comments\n\n";





// next, we'll build the invisible portion of the message body

// note that we insert two dashes in front of the MIME boundary

// when we use it

$message = "This is a multi-part message in MIME format.\n\n" .

"--{$mime_boundary}\n" .

"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .

"Content-Transfer-Encoding: 7bit\n\n" .

$message . "\n\n";

}

// now we'll process our uploaded files

foreach($_FILES as $userfile){

// store the file information to variables for easier access

$tmp_name = $userfile['tmp_name'];

$type = $userfile['type'];

$name = $userfile['name'];

$size = $userfile['size'];



// if the upload succeded, the file will exist

if (file_exists($tmp_name)){



// check to make sure that it is an uploaded file and not a system file

if(is_uploaded_file($tmp_name)){



// open the file for a binary read

$file = fopen($tmp_name,'rb');



// read the file content into a variable

$data = fread($file,filesize($tmp_name));



// close the file

fclose($file);



// now we encode it and split it into acceptable length lines

$data = chunk_split(base64_encode($data));

}



// now we'll insert a boundary to indicate we're starting the attachment

// we have to specify the content type, file name, and disposition as

// an attachment, then add the file content.

// NOTE: we don't set another boundary to indicate that the end of the

// file has been reached here. we only want one boundary between each file

// we'll add the final one after the loop finishes.

$message .= "--{$mime_boundary}\n" .

"Content-Type: {$type};\n" .

" name=\"{$name}\"\n" .

"Content-Disposition: attachment;\n" .

" filename=\"{$fileatt_name}\"\n" .

"Content-Transfer-Encoding: base64\n\n" .

$data . "\n\n";

}

}


// here's our closing mime boundary that indicates the last of the message
$message.="--{$mime_boundary}--\n";

// now we just send the message
// formhandler.php
if (sizeof($_POST) > 1) AND (!$resp->is_valid) { //this is line 213
// process & validate form
// if all data is present and "clean", then proceed
if ( mail($to, $subject, $message, $headers) ) {

// mail success!
header("Location: thankyou.html");

} else {

// mail failure!
header("Location: tryagain.html");
}
}


?>

JAY6390
02-20-2010, 06:06 PM
if (sizeof($_POST) > 1 && !$resp->is_valid) {

jewelsmac66
02-20-2010, 06:28 PM
Thank you for your reply. I did what you said and now it goes to a blank page instead of to the thankyou.html page or tryagain.html page. The form-email also never got delivered to my inbox. Any ideas?

JAY6390
02-20-2010, 06:34 PM
Try this code (I tidied it up somewhat too)

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST")
{

require_once ('recaptchalib.php');
$publickey = "xxxxxxxx";
$privatekey = "xxxxxxx";
$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);


// we'll begin by assigning the To address and message subject
$to = "jmacdesigns@gmail.com";
$subject = "E-mail with attachment";

// get the sender's name and email address
// we'll just plug them a variable to be used later
$from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) .
">";

// generate a random string to be used as the boundary marker
$mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";

// now we'll build the message headers
$headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\"";

// here, we'll start the message body. all entries ARE case sensitive!!
// this is the text that will be displayed
// in the e-mail
$message = $_POST['message'];
$YourName = $_POST['YourName'];
$BizName = $_POST['BizName'];
$address = $_POST['address'];
$city = $_POST['city'];
$State = $_POST['State'];
$zipcode = $_POST['zipcode'];
$email = $_POST['email'];
$JobType = $_POST['JobType'];
$JobDimensions = $_POST['JobDimensions'];
$Quantity = $_POST['Quantity'];
$PaperWhite = $_POST['PaperWhite'];
$PaperOffWhite = $_POST['PaperOffWhite'];
$PaperCSWhite = $_POST['PaperCSWhite'];
$PaperCSOffWhite = $_POST['PaperCSOffWhite'];
$PaperOther = $_POST['PaperOther'];
$LaminationYes = $_POST['LaminationYes'];
$LaminationNo = $_POST['LaminationNo'];
$DateReq = $_POST['DateReq'];
$comments = $_POST['comments'];

$message = "Your Name: $YourName\n\nBusiness Name: $BizName\n\nAddress: $Address\n\nCity: $City\n\nState: $State\n\nZip Code: $ZipCode\n\nEmail: $email\n\nJob Type: $JobType\n\nJob Dimensions: $JobDimensions\n\nQuantity: $Quantity\n\nPaper White: $PaperWhite\n\nPaper Off White: $PaperOffWhite\n\nPaper Card Stock White: $PaperCSWhite\n\nPaper Card Stock Off White: $PaperCSOffWhite\n\nPaper Other: $PaperOther\n\nLamination Yes: $LaminationYes\n\nLamination No: $LaminationNo\n\nDate Required: $DateReq\n\nComments: $comments\n\n";


// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
}
// now we'll process our uploaded files
foreach ($_FILES as $userfile)
{

// store the file information to variables for easier access

$tmp_name = $userfile['tmp_name'];
$type = $userfile['type'];
$name = $userfile['name'];
$size = $userfile['size'];

// if the upload succeded, the file will exist
if (file_exists($tmp_name))
{
// check to make sure that it is an uploaded file and not a system file
if (is_uploaded_file($tmp_name))
{
// open the file for a binary read
$file = fopen($tmp_name, 'rb');

// read the file content into a variable
$data = fread($file, filesize($tmp_name));

// close the file
fclose($file);

// now we encode it and split it into acceptable length lines
$data = chunk_split(base64_encode($data));
}


// now we'll insert a boundary to indicate we're starting the attachment
// we have to specify the content type, file name, and disposition as
// an attachment, then add the file content.
// NOTE: we don't set another boundary to indicate that the end of the
// file has been reached here. we only want one boundary between each file
// we'll add the final one after the loop finishes.
$message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$name}\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}

// here's our closing mime boundary that indicates the last of the message
$message .= "--{$mime_boundary}--\n";

// now we just send the message
// formhandler.php
if (sizeof($_POST) > 1 && $resp->is_valid)
{ //this is line 213
// process & validate form
// if all data is present and "clean", then proceed
if (mail($to, $subject, $message, $headers))
{
// mail success!
header("Location: thankyou.html");

} else {
// mail failure!
header("Location: tryagain.html");
}
}

?>

jewelsmac66
02-20-2010, 06:37 PM
Thank you so much for doing that, I appreciate it! But the form is still going to a blank page...? Here is the link to the form that calls on the formhandler2.php we are working on: http://www.jewelsdesignworks.com/apptools4.php

jewelsmac66
02-20-2010, 07:46 PM
I apologize, I forgot to add back in my key numbers to that code. My mistake, I'm sorry!

I added the keys back in, and the form DID redirect to the thankyou.html page, however, the form-email was not delivered to my inbox.

I wonder why will it not send the form, even though it appears to pass the validation process by going through to the thank you page?

jewelsmac66
02-22-2010, 05:06 PM
Is it not sending the form, despite going through to the thankyou.html page, because it needs another set of () in the code line?

Currently it says this at the end:


if (sizeof($_POST) > 1 && $resp->is_valid)
{
// process & validate form
// if all data is present and "clean", then proceed
if (mail($to, $subject, $message, $headers))
{
// mail success!
header("Location: thankyou.html");

} else {
// mail failure!
header("Location: tryagain.html");
}
}
?>


So I just tried this, and it went to a blank page again, and it still did not deliver the form to my inbox:


if (sizeof($_POST) > 1 && ($resp->is_valid))


And I also just tried this:


if (sizeof($_POST) > 1) && ($resp->is_valid)

And it gave me this error:
Parse error: syntax error, unexpected T_BOOLEAN_AND in /home/content/j/e/w/jewelsmac6/html/formhandler2.php on line 111
The line above is line 111.

Any suggestions? Thank you in advance!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum