Page 1 of 2
combining codes?
Posted: Thu Feb 02, 2012 6:16 am
by nitrotrike
Hi, new to the board and have an interesting dilemma that is requiring me to combine two separate codes. I'm hoping someone has had to do this before with this particular script.
The critical issue is that I have a submit form on my site, which refers to a php file to process and email it. Seemed easy enough. I used this "form to mail" script -
http://www.web4future.com/easiest-form2mail.htm.
Now, I want to weed out the spam bots with recaptcha and I am absolutely lost as to how to combine the codes. I have all of my necessary keys from recaptcha. The roadblock is that the reCaptcha coding wants to post to "verify.php", yet the existing form wants to post to "form2mail.php".
The second, yet much less important part is that the form2mail will send blank fields (I have multiple lines where customers can enter variables from a dropdown and enter text). I have already figured out how to make the name, address, etc "required", but cannot do it to the others.
Ideas?
Re: combining codes?
Posted: Thu Feb 02, 2012 6:22 am
by Celauran
Without seeing the code? Not really. I'd guess it would be simplest to add a mail() call into verify.php.
Re: combining codes?
Posted: Thu Feb 02, 2012 6:28 am
by nitrotrike
Which code do you need to see, the form2mail, or the recatpcha? I wasn't sure if I was allowed to fill the window with codes or not. The recaptcha is the generic code that Google gives you. I'm very new to php, and don't know where to add things, hence my initial post.
Re: combining codes?
Posted: Thu Feb 02, 2012 6:37 am
by Celauran
Ideally both. Remember to use the syntax tags before posting code (see PHP Code button) or it will be an illegible mess.
Re: combining codes?
Posted: Thu Feb 02, 2012 6:52 am
by nitrotrike
OK, makes sense..
My form posts to this:
Code: Select all
<?php
# You can use this script to submit your forms or to receive orders by email.
$MailToAddress = "myemail@mydomain.com"; // your email address
$redirectURL = "thank-you.html"; // the URL of the thank you page.
$MailSubject = "[Message from the contact form]"; // the subject of the email
$sendHTML = TRUE; //set to "false" to receive Plain TEXT e-mail
$serverCheck = FALSE; // if, for some reason you can't send e-mails, set this to "false"
# copyright 2006 Web4Future.com =================== READ THIS ===================================================
# If you are asking for a name and an email address in your form, you can name the input fields "name" and "email".
# If you do this, the message will appear to come from that email address and you can simply click the reply button to answer it.
# To block an IP, simply add it to the blockip.txt text file.
# CHMOD 777 the blockip.txt file (run "CHMOD 777 blockip.txt", without the double quotes)
# This is needed because the script tries to block the IP that tried to hack it
# If you have a multiple selection box or multiple checkboxes, you MUST name the multiple list box or checkbox as "name[]" instead of just "name"
# you must also add "multiple" at the end of the tag like this: <select name="myselectname[]" multiple>
# you have to do the same with checkboxes
/*****************************************************************
Web4Future Easiest Form2Mail (GPL).
Copyright (C) 1998-2006 Web4Future.com All Rights Reserved.
http://www.Web4Future.com/
This script was written by George L. & Calin S. from Web4Future.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*****************************************************************/
# DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING ===================================================
$w4fver = "2.2";
$ip = ($_SERVER['HTTP_X_FORWARDED_FOR'] == "" ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
//function blockIP
function blockip($ip) {
$handle = @fopen("blockip.txt", 'a');
@fwrite($handle, $ip."\n");
@fclose($handle);
}
$w4fx = stristr(file_get_contents('blockip.txt'),getenv('REMOTE_ADDR'));
if ($serverCheck) {
if (preg_match ("/".str_replace("www.", "", $_SERVER["SERVER_NAME"])."/i", $_SERVER["HTTP_REFERER"])) { $w4fy = TRUE; } else { $w4fy = FALSE; }
} else { $w4fy = TRUE; }
if (($w4fy === TRUE) && ($w4fx === FALSE)) {
$w4fMessage = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html>\n<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head><body><font face=3Dverdana size=3D2>";
if (count($_GET) >0) {
reset($_GET);
while(list($key, $val) = each($_GET)) {
$GLOBALS[$key] = $val;
if (is_array($val)) {
$w4fMessage .= "<b>$key:</b> ";
foreach ($val as $vala) {
$vala =stripslashes($vala);
$vala = htmlspecialchars($vala);
if (trim($vala)) { if (stristr($vala,"Content-Type:") || stristr($vala,"MIME-Version") || stristr($vala,"Content-Transfer-Encoding") || stristr($vala,"bcc:")) { blockip($ip); die("ILLEGAL EXECUTION DETECTED!"); } }
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "<br>\n";
}
else {
$val = stripslashes($val);
if (trim($val)) { if (stristr($val,"Content-Type:") || stristr($val,"MIME-Version") || stristr($val,"Content-Transfer-Encoding") || stristr($val,"bcc:")) { blockip($ip); die("ILLEGAL EXECUTION DETECTED!"); } }
if (($key == "Submit") || ($key == "submit")) { }
else { if ($val == "") { $w4fMessage .= "$key: - <br>\n"; }
else { $w4fMessage .= "<b>$key:</b> $val<br>\n"; }
}
}
} // end while
}//end if
else {
reset($_POST);
while(list($key, $val) = each($_POST)) {
$GLOBALS[$key] = $val;
if (is_array($val)) {
$w4fMessage .= "<b>$key:</b> ";
foreach ($val as $vala) {
$vala =stripslashes($vala);
$vala = htmlspecialchars($vala);
if (trim($vala)) { if (stristr($vala,"Content-Type:") || stristr($vala,"MIME-Version") || stristr($vala,"Content-Transfer-Encoding") || stristr($vala,"bcc:")) {blockip($ip); die("ILLEGAL EXECUTION DETECTED!"); } }
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "<br>\n";
}
else {
$val = stripslashes($val);
if (trim($val)) { if (stristr($val,"Content-Type:") || stristr($val,"MIME-Version") || stristr($val,"Content-Transfer-Encoding") || stristr($val,"bcc:")) {blockip($ip); die("ILLEGAL EXECUTION DETECTED!"); } }
if (($key == "Submit") || ($key == "submit")) { }
else { if ($val == "") { $w4fMessage .= "$key: - <br>\n"; }
else { $w4fMessage .= "<b>$key:</b> $val<br>\n"; }
}
}
} // end while
}//end else
$w4fMessage .= "<font size=3D1><br><br>\n Sender IP: ".$ip."</font></font></body></html>";
$w4f_what = array("/To:/i", "/Cc:/i", "/Bcc:/i","/Content-Type:/i","/\n/");
$name = preg_replace($w4f_what, "", $name);
$email = preg_replace($w4f_what, "", $email);
if (!$email) {$email = $MailToAddress;}
$mailHeader = "From: $name <$email>\r\n";
$mailHeader .= "Reply-To: $name <$email>\r\n";
$mailHeader .= "Message-ID: <". md5(rand()."".time()) ."@". ereg_replace("www.","",$_SERVER["SERVER_NAME"]) .">\r\n";
$mailHeader .= "MIME-Version: 1.0\r\n";
if ($sendHTML) {
$mailHeader .= "Content-Type: multipart/alternative;";
$mailHeader .= " boundary=\"----=_NextPart_000_000E_01C5256B.0AEFE730\"\r\n";
}
$mailHeader .= "X-Priority: 3\r\n";
$mailHeader .= "X-Mailer: PHP/" . phpversion()."\r\n";
$mailHeader .= "X-MimeOLE: Produced By Web4Future Easiest Form2Mail $w4fver\r\n";
if ($sendHTML) {
$mailMessage = "This is a multi-part message in MIME format.\r\n\r\n";
$mailMessage .= "------=_NextPart_000_000E_01C5256B.0AEFE730\r\n";
$mailMessage .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n";
$mailMessage .= trim(strip_tags($w4fMessage))."\r\n\r\n";
$mailMessage .= "------=_NextPart_000_000E_01C5256B.0AEFE730\r\n";
$mailMessage .= "Content-Type: text/html; charset=\"ISO-8859-1\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n";
$mailMessage .= "$w4fMessage\r\n\r\n";
$mailMessage .= "------=_NextPart_000_000E_01C5256B.0AEFE730--\r\n";
}
if ($sendHTML === FALSE) {
$mailHeader .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n";
$mailMessage .= trim(strip_tags($w4fMessage))."\r\n\r\n";
}
if (!mail($MailToAddress, $MailSubject, $mailMessage,$mailHeader)) { echo "Error sending e-mail!";}
else { header("Location: ".$redirectURL); }
} else { echo "<center><font face=verdana size=3 color=red><b>ILLEGAL EXECUTION DETECTED!</b></font></center>";}
?>
The recaptcha code wants to post my form to this, per their instructions of "The following code should be placed at the top of the verify.php file":
Code: Select all
<?php
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
}
?>
This is the form code they say to use:
<html>
<body> <!-- the body tag is required or the CAPTCHA may not show on some browsers -->
<!-- your HTML content -->
<form method="post" action="verify.php">
<?php
require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<input type="submit" />
</form>
<!-- more of your HTML content -->
</body>
</html>
Re: combining codes?
Posted: Thu Feb 02, 2012 7:06 am
by Celauran
Code: Select all
<?php
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
// Your code here to handle a successful verification
// ie. Copy the other script here
}
?>
The page with the form:
Code: Select all
<?php
require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Debug</title>
</head>
<body>
<form action="verify.php" method="post">
<!-- Other form fields go here -->
<input type="text" name="whatever" />
<?php echo recaptcha_get_html($publickey); ?>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Note that these can be the same page. Simply leave the form action empty and add the HTML beneath the PHP code above.
Re: combining codes?
Posted: Thu Feb 02, 2012 7:22 am
by nitrotrike
Maybe I'm missing something here and please pardon my lack of knowledge, but how will my form get sent to the form2mail.php then? Do I place the entire form where it says "other form fields here", such as <form> -content- </form>, like a form within a form?
Re: combining codes?
Posted: Thu Feb 02, 2012 8:18 am
by Celauran
nitrotrike wrote:Maybe I'm missing something here and please pardon my lack of knowledge, but how will my form get sent to the form2mail.php then?
Technically it won't. That doesn't matter, though, since you've copied the contents of that file into verify.php where I indicated above.
nitrotrike wrote:Do I place the entire form where it says "other form fields here", such as <form> -content- </form>, like a form within a form?
Omit the form tags and submit button, include everything else.
Re: combining codes?
Posted: Thu Feb 02, 2012 8:41 am
by juanjesse
<?php is a great programming language specially for web applications. Combined it with HTML5 and MySQL for advance dynamic website.
Very Informative
thanks
central heating
Re: combining codes?
Posted: Thu Feb 02, 2012 8:46 am
by nitrotrike
This is the point I was at last night. If I leave the form action blank, it does nothing. If I label it as verify.php, i constantly get the incorrect captcha was entered warning. Also, If I omit the submit button... how will i submit the captcha code? This is getting more and more confusing to the point I think I'd rather just deal with the spam lol
Re: combining codes?
Posted: Thu Feb 02, 2012 8:53 am
by Celauran
nitrotrike wrote:Also, If I omit the submit button... how will i submit the captcha code?
You don't omit the submit button altogether; I simply said not to copy it as the form I posted above already included one.
nitrotrike wrote:This is the point I was at last night. If I leave the form action blank, it does nothing.
It posts the information to the current page, meaning you need to have the form, the captcha code, and the form processing code all on the same page. If you'd rather not do it that way, that's fine. Leave the action as verify.php, copy the body of form2mail.php into verify.php where I indicated and you should be done.
Re: combining codes?
Posted: Thu Feb 02, 2012 9:01 am
by nitrotrike
I'm either putting the form2mail under the verify.pho script incorrectly or something... do I need to remove the "// your code here to handle..." lines first? Also, If I used the code you posted, I get another text input field under the captcha box, which already has its own.
Re: combining codes?
Posted: Thu Feb 02, 2012 9:15 am
by Celauran
Post everything you've got and I'll reorder it as needed.
Re: combining codes?
Posted: Thu Feb 02, 2012 9:24 am
by nitrotrike
I appreciate the help!
Both the verify and form2mail are posted above.
To display the captcha box, they give me the last code under those.
Re: combining codes?
Posted: Thu Feb 02, 2012 9:28 am
by Celauran
Name this whole mess verify.php, keep your form separate with action="verify.php" and it should work.
Code: Select all
<?php
require_once('recaptchalib.php');
$privatekey = "your_private_key";
$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
// What happens when the CAPTCHA was entered incorrectly
die("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");
}
else
{
# You can use this script to submit your forms or to receive orders by email.
$MailToAddress = "myemail@mydomain.com"; // your email address
$redirectURL = "thank-you.html"; // the URL of the thank you page.
$MailSubject = "[Message from the contact form]"; // the subject of the email
$sendHTML = TRUE; //set to "false" to receive Plain TEXT e-mail
$serverCheck = FALSE; // if, for some reason you can't send e-mails, set this to "false"
# copyright 2006 Web4Future.com =================== READ THIS ===================================================
# If you are asking for a name and an email address in your form, you can name the input fields "name" and "email".
# If you do this, the message will appear to come from that email address and you can simply click the reply button to answer it.
# To block an IP, simply add it to the blockip.txt text file.
# CHMOD 777 the blockip.txt file (run "CHMOD 777 blockip.txt", without the double quotes)
# This is needed because the script tries to block the IP that tried to hack it
# If you have a multiple selection box or multiple checkboxes, you MUST name the multiple list box or checkbox as "name[]" instead of just "name"
# you must also add "multiple" at the end of the tag like this: <select name="myselectname[]" multiple>
# you have to do the same with checkboxes
/* * ***************************************************************
Web4Future Easiest Form2Mail (GPL).
Copyright (C) 1998-2006 Web4Future.com All Rights Reserved.
http://www.Web4Future.com/
This script was written by George L. & Calin S. from Web4Future.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
* *************************************************************** */
# DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING ===================================================
$w4fver = "2.2";
$ip = ($_SERVER['HTTP_X_FORWARDED_FOR'] == "" ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']);
//function blockIP
function blockip($ip)
{
$handle = @fopen("blockip.txt", 'a');
@fwrite($handle, $ip . "\n");
@fclose($handle);
}
$w4fx = stristr(file_get_contents('blockip.txt'), getenv('REMOTE_ADDR'));
if ($serverCheck)
{
if (preg_match("/" . str_replace("www.", "", $_SERVER["SERVER_NAME"]) . "/i", $_SERVER["HTTP_REFERER"]))
{
$w4fy = TRUE;
}
else
{
$w4fy = FALSE;
}
}
else
{
$w4fy = TRUE;
}
if (($w4fy === TRUE) && ($w4fx === FALSE))
{
$w4fMessage = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html>\n<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head><body><font face=3Dverdana size=3D2>";
if (count($_GET) > 0)
{
reset($_GET);
while (list($key, $val) = each($_GET))
{
$GLOBALS[$key] = $val;
if (is_array($val))
{
$w4fMessage .= "<b>$key:</b> ";
foreach ($val as $vala)
{
$vala = stripslashes($vala);
$vala = htmlspecialchars($vala);
if (trim($vala))
{
if (stristr($vala, "Content-Type:") || stristr($vala, "MIME-Version") || stristr($vala, "Content-Transfer-Encoding") || stristr($vala, "bcc:"))
{
blockip($ip);
die("ILLEGAL EXECUTION DETECTED!");
}
}
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "<br>\n";
}
else
{
$val = stripslashes($val);
if (trim($val))
{
if (stristr($val, "Content-Type:") || stristr($val, "MIME-Version") || stristr($val, "Content-Transfer-Encoding") || stristr($val, "bcc:"))
{
blockip($ip);
die("ILLEGAL EXECUTION DETECTED!");
}
}
if (($key == "Submit") || ($key == "submit"))
{
}
else
{
if ($val == "")
{
$w4fMessage .= "$key: - <br>\n";
}
else
{
$w4fMessage .= "<b>$key:</b> $val<br>\n";
}
}
}
} // end while
}//end if
else
{
reset($_POST);
while (list($key, $val) = each($_POST))
{
$GLOBALS[$key] = $val;
if (is_array($val))
{
$w4fMessage .= "<b>$key:</b> ";
foreach ($val as $vala)
{
$vala = stripslashes($vala);
$vala = htmlspecialchars($vala);
if (trim($vala))
{
if (stristr($vala, "Content-Type:") || stristr($vala, "MIME-Version") || stristr($vala, "Content-Transfer-Encoding") || stristr($vala, "bcc:"))
{
blockip($ip);
die("ILLEGAL EXECUTION DETECTED!");
}
}
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "<br>\n";
}
else
{
$val = stripslashes($val);
if (trim($val))
{
if (stristr($val, "Content-Type:") || stristr($val, "MIME-Version") || stristr($val, "Content-Transfer-Encoding") || stristr($val, "bcc:"))
{
blockip($ip);
die("ILLEGAL EXECUTION DETECTED!");
}
}
if (($key == "Submit") || ($key == "submit"))
{
}
else
{
if ($val == "")
{
$w4fMessage .= "$key: - <br>\n";
}
else
{
$w4fMessage .= "<b>$key:</b> $val<br>\n";
}
}
}
} // end while
}//end else
$w4fMessage .= "<font size=3D1><br><br>\n Sender IP: " . $ip . "</font></font></body></html>";
$w4f_what = array("/To:/i", "/Cc:/i", "/Bcc:/i", "/Content-Type:/i", "/\n/");
$name = preg_replace($w4f_what, "", $name);
$email = preg_replace($w4f_what, "", $email);
if (!$email)
{
$email = $MailToAddress;
}
$mailHeader = "From: $name <$email>\r\n";
$mailHeader .= "Reply-To: $name <$email>\r\n";
$mailHeader .= "Message-ID: <" . md5(rand() . "" . time()) . "@" . ereg_replace("www.", "", $_SERVER["SERVER_NAME"]) . ">\r\n";
$mailHeader .= "MIME-Version: 1.0\r\n";
if ($sendHTML)
{
$mailHeader .= "Content-Type: multipart/alternative;";
$mailHeader .= " boundary=\"----=_NextPart_000_000E_01C5256B.0AEFE730\"\r\n";
}
$mailHeader .= "X-Priority: 3\r\n";
$mailHeader .= "X-Mailer: PHP/" . phpversion() . "\r\n";
$mailHeader .= "X-MimeOLE: Produced By Web4Future Easiest Form2Mail $w4fver\r\n";
if ($sendHTML)
{
$mailMessage = "This is a multi-part message in MIME format.\r\n\r\n";
$mailMessage .= "------=_NextPart_000_000E_01C5256B.0AEFE730\r\n";
$mailMessage .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n";
$mailMessage .= trim(strip_tags($w4fMessage)) . "\r\n\r\n";
$mailMessage .= "------=_NextPart_000_000E_01C5256B.0AEFE730\r\n";
$mailMessage .= "Content-Type: text/html; charset=\"ISO-8859-1\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n";
$mailMessage .= "$w4fMessage\r\n\r\n";
$mailMessage .= "------=_NextPart_000_000E_01C5256B.0AEFE730--\r\n";
}
if ($sendHTML === FALSE)
{
$mailHeader .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n";
$mailMessage .= trim(strip_tags($w4fMessage)) . "\r\n\r\n";
}
if (!mail($MailToAddress, $MailSubject, $mailMessage, $mailHeader))
{
echo "Error sending e-mail!";
}
else
{
header("Location: " . $redirectURL);
}
}
else
{
echo "<center><font face=verdana size=3 color=red><b>ILLEGAL EXECUTION DETECTED!</b></font></center>";
}
}
?>