[RESOLVED] Is this php code correct for emailing with a Captcha??
PHP Code:
<?php
require_once('recaptchalib.php');
$publickey = "my_publickey";
$privatekey = "my_privatekey";
$resp = null;
$error = null;
if ($_POST["recaptcha_response_field"]) {
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$company = $_REQUEST['company'] ;
$address = $_REQUEST['address'] ;
$city = $_REQUEST['city'] ;
$state = $_REQUEST['state'] ;
$zip = $_REQUEST['zip'] ;
$phone = $_REQUEST['phone'] ;
$sendsale = $_REQUEST['sendsalespacket'] ;
$keep = $_REQUEST['keepmeupdated'] ;
if ( ereg( "[\r\n]", $name) || ereg("[\r\n]", $email ) ) {
header( "Location: error.htm" );
}
mail("[email protected]", "Request Packet", $message, "Name: $name\nCompany: $company\nAddress: $address\nCity: $city\nState: $state\nZip Code: $zip\nPhone: $phone\nEmail: $email\nComments: $message\nSend Sales Packet: $sendsale\nKeep Me Informed: $keep") ;
header( "Location: contacts.htm" ) ;
} else {
# set the error code so that we can display it
header( "Location: error.htm" ) ;
}
}
echo recaptcha_get_html($publickey, $error);
?>
Re: Is this php code correct for emailing with a Captcha??
When you run it, does it email properly?
Are you receiving an error or what?
The code there runs through a couple functions which I assume are in recaptchalib.php and then uses the mail function to send an email to "[email protected]"
Re: Is this php code correct for emailing with a Captcha??
When you type in the captcha codes correctly or incorrectly it goes to the contacts page.
Re: Is this php code correct for emailing with a Captcha??
well, then the recaptcha_check_answer() function is at fault (because that's the function that dictates whether or not to go to contacts.htm), which I assume is included in the include you've omitted. I doubt anyone will be able to help you without it.
Re: Is this php code correct for emailing with a Captcha??
I have abandoned the recaptcha use. I found a site that shows how to create your own.
THIS Code does not seem to be working and am wondering what things I should be looking for. I am new to PHP so.......
Code:
<?php
if(isset($_POST['captcha'])){
if(strtolower($_POST['captcha']) == strtolower($_SESSION['captcha'])){
echo '<div>Success!</div>';
}
else{
echo '<div>Incorrect</div>';
}
}
?>
This is residing here on my request information page.
Code:
<?php session_start(); ?>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Four Lakes Business Systems - Request Information</title>
<script src="menuscript.js" language="javascript" type="text/javascript">function TEXTAREA1_onclick() {
}
</script>
<link rel="stylesheet" type="text/css" href="menustyle.css" media="screen, print" />
</head>
<body bgcolor=darkgray>
<?php
if(isset($_POST['captcha'])){
if(strtolower($_POST['captcha']) == strtolower($_SESSION['captcha'])){
echo '<div>Success!</div>';
}
else{
echo '<div>Incorrect</div>';
}
}
?>
<form method="post" action="sendmail.php">
Re: [RESOLVED] Is this php code correct for emailing with a Captcha??
like stated in my previous post, you're omitting all of the relevant information needed to help you (eg. the script that you're posting from).
from what you've given me, I can only conclude that maybe your session variable is not being set correctly. you could also have started your session after already outputting something to the browser -- session_start() can only be used before this has happened.
Don't give up on reCaptcha
Your noble endeavor was almost complete.
Make sure you:
1) put it in a proper HTML page
2) include an HTML Form tag
3) the Form tag uses the POST method
4) *You DID register for a key right?*
like so:
Code:
<?php
require_once('recaptchalib.php');
$publickey = "my_publickey";
$privatekey = "my_privatekey";
$resp = null;
$error = null;
if ($_POST["recaptcha_response_field"]) {
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$company = $_REQUEST['company'] ;
$address = $_REQUEST['address'] ;
$city = $_REQUEST['city'] ;
$state = $_REQUEST['state'] ;
$zip = $_REQUEST['zip'] ;
$phone = $_REQUEST['phone'] ;
$sendsale = $_REQUEST['sendsalespacket'] ;
$keep = $_REQUEST['keepmeupdated'] ;
if ( ereg( "[\r\n]", $name) || ereg("[\r\n]", $email ) ) {
header( "Location: error.htm" );
}
mail("[email protected]", "Request Packet", $message, "Name: $name\nCompany: $company\nAddress: $address\nCity: $city\nState: $state\nZip Code: $zip\nPhone: $phone\nEmail: $email\nComments: $message\nSend Sales Packet: $sendsale\nKeep Me Informed: $keep") ;
header( "Location: contacts.htm" ) ;
} else {
# set the error code so that we can display it
header( "Location: error.htm" ) ;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>VBForums mojo69 Testing...</title>
</head>
<body>
<form action="" method="post">
<?php
echo recaptcha_get_html($publickey, $error);
?>
</form>
</body>
</html>
Normally I split my display page with the public key, and the validate page with the private key outside of the webroot. If your web server is ever misconfigured, it could display your PHP code revealing your keys.
I actually do not use the recaptcha_get_html call with the $error. You might as well not also since you redirect to your custom error or success pages. If you don't redirect to your error page upon error, then I think reCaptcha will display an error for you if you use the call as you had "recaptcha_get_html($publickey, $error);"
On your workaround custom method without reCaptcha, you probably did not set your SESSION variable.