Hi there,

I'm currently adding in a recaptcha to a contact form that I didn't create in the first place, so I've been tentatively working my way around it. For some reason though, every time I complete my form, no matter whether the captcha I entered was right or not, it's coming up with the "incorrect response" (for the time being I have set google.co.uk as the "incorrect captcha" response, just for testing purposes). So it goes to google.co.uk every time I press submit.

I've put all the code for the two pages below. Hope you can help! Please bear in mind that I am pretty much a novice with this stuff and just working my way through tutorials and stuff, so any help would be great. I found an answer on this page (http://code.google.com/p/recaptcha/wiki/FAQ#I_keep_getting_%22incorrect-captcha-sol%22_even_though_I&#) which could help but I don't really understand it. Could anyone explain?

Here is the code for the page - www.electrix.co.uk/contact2.php

<?php require('sbsquared.class.php'); ?> 
<?php 
if(!empty($_GET['lang'])) { 
        switch($_GET['lang']) { 
                case 'fr': 
                        setlocale(LC_ALL, 'fr_FR.utf8'); 
                        $_SESSION['language'] = 'fr'; 
                        break; 
                case 'us': 
                        setlocale(LC_ALL, 'en_EN.utf8'); 
                        $_SESSION['language'] = 'us'; 
                        break; 
                case 'eu': 
                        setlocale(LC_ALL, 'en_EN.utf8'); 
                        $_SESSION['language'] = 'eu'; 
                        break; 
                case 'de': 
                        setlocale(LC_ALL, 'de_DE.utf8'); 
                        $_SESSION['language'] = 'de'; 
                        break; 
                default: 
                        setlocale(LC_ALL, 'en_EN.utf8'); 
                        $_SESSION['language'] = 'en'; 
        } 
} 

$sb = New SBSquared; 
$content = '<div id="main_middle"> 
<style> 
#sbtbtn{ 
        margin:0; 
        padding: 0; 
        list-style:none; 
        } 
#sbtbtn a{ 
        margin-top: 2px; 
        background: #80b0fc url(./images/right_arrow_light.gif) top right no- 
repeat; 
        display:block; 
        padding: 2px; 
        font-family:arial; 
        color:#fff; 
        text-decoration:none; 
        font-size:8pt; 
        font-weight:400; 
        margin-right:5px; 
        width:143px; 
        } 
#sbtbtn a.darkblue{ 
        margin-top: 2px; 
        background: #005ff8 url(./images/right_arrow_dark.gif) top right no- 
repeat; 
        display:block; 
        padding: 1px; 
        font-family:arial; 
        color:#fff; 
        text-decoration:none; 
        font-size:110%; 
        font-weight:400; 
        margin-right:10px; 
        width:130px; 
        padding-left: 2px; 
        } 
</style> 
<script language = "javascript"> 
function verifyEnEnquiry(theform) { 
        var my_message = "default"; 
        if(document.enquiries.FullName.value == "") { 
                alert("'.$sb->dt('Sorry, but you cannot proceed unless you complete 
your Full Name').'"); 
                //return false; 
        } else 
        if(document.enquiries.Company.value == "") { 
                alert("'.$sb->dt('Sorry, but you cannot proceed unless you complete 
your Company Name').'"); 
                //return false; 
        } else 
        if(document.enquiries.Email.value == "") { 
                alert("'.$sb->dt('Sorry, but you cannot proceed unless you complete 
your Email ').'"); 
                //return false; 
        } else 
        if(document.enquiries.address_4.value == "") { 
                alert("'.$sb->dt('Sorry, but you cannot proceed unless you complete 
your City').'"); 
                //return false; 
        } else 
        if(document.enquiries.Enquiry.value == "") { 
                alert("'.$sb->dt('Sorry, but you cannot proceed unless you complete 
your Enquiry ').'"); 
                //return false; 
        } else 
        if(document.enquiries.JobTitle.value == "") { 
                alert("'.$sb->dt('Sorry, but you cannot proceed unless you complete 
your Job Title').'"); 
                //return false; 
        } else 
        if(document.enquiries.country.selectedIndex == 0) 
        { 
                alert("'.$sb->dt('Sorry, but you cannot proceed unless you complete 
your Country').'"); 
                //return false; 
        } else 
        if(document.enquiries.address_1.value == "") { 
                alert("'.$sb->dt('Sorry, but you cannot proceed unless you complete 
your Address').'"); 
                //return false; 
        } else 
        if(document.enquiries.Postcode.value == "") { 
                alert("'.$sb->dt('Sorry, but you cannot proceed unless you complete 
your Postcode').'"); 
                //return false; 
        } 
        else { 
                //return true; 
                document.enquiries.submit(); 
        } 
        //alert(my_message); 
} 

</script> 
<!-- Start Dynamic Content --> 
'; 
$conn = $sb->openConnection(); 
if($_SESSION['language'] == "eu") { 
        $lang = "en"; 
} 

else { 
        $lang = $_SESSION['language']; 
} 

$sql = "SELECT * FROM `content_pages` WHERE `page` = 'contact' AND 
`lang` = '".$lang."'"; 
$result = mysql_query($sql, $conn) or die(mysql_error()); 
require_once('recaptchalib.php'); 
$publickey = "6LeiEskSAAAAAHnRLNDQS31tOEzWlGQzOabB8i_R"; // you got 
this from the signup page 
$captcha = recaptcha_get_html($publickey); 
$page_content = mysql_fetch_array($result); 
$content .= $page_content['content']; 
$content .= ' 
<!-- End Dynamic Content --> 
 <script type="text/javascript"> 
 var RecaptchaOptions = { 
    theme : "white" 
 }; 
 </script> 
<div class="boxheader">'.$sb->dt('Enquiry Form').'</div> 
<form style = "margin: 0px" name="enquiries" method="post" 
onSubmit="return verifyEnEnquiry(this);" action="verify.php"> 
</div> 
<table style = "margin-left: 11px; width: 383px"> 
<input type="hidden" name="Location" value="verify.php"> 
<tr> 
<style> 
        th { 
                 padding-left: 8px; 
                } 
</style> 
<th>'.$sb->dt("Enquiry Type *").'</th> 
<td align = "right"> 
<select name="EnquiryType" style="font-size:100%;width:202px;border: 
1px solid #808080" /> 
<option value="Sales">'.$sb->dt('Sales').'</option> 
<option value="Technical">'.$sb->dt('Technical').'</option> 
<option value="Quotations">'.$sb->dt('Quotations').'</option> 
<option value="General Enquiry">'.$sb->dt('General Enquiry').'</ 
option> 
<option value="Literature Request">'.$sb->dt('Literature Request').'</ 
option> 
</select> 
</td> 
</tr> 
<tr> 
<th>'.$sb->dt("Full Name *").'</th> 
<td align = "right"><input name="FullName" type="text" id="FullName" 
style="width:200px" /></td> 
</tr> 
<tr> 
<th>'.$sb->dt("Company *").'</th> 
<td align = "right"><input name="Company" type="text" id="Company" 
style="width:200px" /></td> 
</tr> 
<tr> 
<th>'.$sb->dt("Job Title *").'</th> 
<td align = "right"><input name="JobTitle" type="text" id="JobTitle" 
style="width:200px" /></td> 
</tr> 
<tr> 
<th>'.$sb->dt("Address *").'</th> 
<td align = "right"><input type="text" name="address_1" style="width: 
200px" /></td> 
</tr> 
<tr> 
<th></th> 
<td align = "right"><input type="text" name="address_2" style="width: 
200px" /></td> 
</tr> 
<tr> 
<th></th> 
<td align = "right"><input type="text" name="address_3" style="width: 
200px" /></td> 
</tr> 
<tr> 
<th>'.$sb->dt("City *").'</th> 
<td align = "right"><input type="text" name="address_4" style="width: 
200px" /></td> 
</tr> 
<tr> 
<th>'.$sb->dt("Postal Code *").'</th> 
<td align = "right"><input name="Postcode" type="text" id="Postcode" 
style="width:200px" /></td> 
</tr> 
<th>'.$sb->dt("Country *").'</th> 
<td align = "right"> <select name="country" style="font-size:90%;width: 
202px;border:1px solid #808080"> 
'.genCountryList().' 
</select></td> 
</tr> 
<tr> 
<th>'.$sb->dt("Telephone Number").'</th> 
<td align = "right"><input name="Telephone" type="text" id="Telephone" 
style="width:200px" /></td> 
</tr> 
<tr> 
<th>'.$sb->dt("Email *").'</th> 
<td align = "right"><input name="Email" type="text" id="Email" 
style="width:200px" /></td> 
</tr> 
<tr> 
<th>'.$sb->dt("Fax Number").'</th> 
<td align = "right"><input name="Fax" type="text" id="Fax" 
style="width:200px" /></td> 
</tr> 
<tr> 
<th>'.$sb->dt("Website").'</th> 
<td align = "right"><input name="Website" type="text" id="Website" 
style="width:200px" /></td> 
</tr> 
<tr> 
<th>'.$sb->dt("Enquiry *").'</th> 
<td align = "right"><textarea name="Enquiry" rows="8" style="font-size: 
90%;width:200px;border:1px solid #808080; font: 9pt arial"></ 
textarea></td> 
</tr> 
<!--<tr> 
<th>Send a File (Max. 2Mb)</th> 
<td align = "right"><input name="FileUpload" type="file" 
id="FileUpload" style="width:160px" /></td> 
</tr>--> 
<tr> 
<th colspan="2" align="center"> 
'. $captcha .' 
</th> 
</tr> 
<tr> 
<th>&nbsp;</th> 
<td>&nbsp;</td> 
</tr> 
</table> 
<center><table width = "100%" style = "border: 0px; width: 
395px"><tr><td align = "right"> 
<ul id="sbtbtn"> 
<li><a style = "margin-top: 5px; text-align: left; font: 8pt arial;" 
href="Javascript:verifyEnEnquiry(document.enquiries);" title="'.$sb- 
>dt('Send Enquiry').'" class="darkblue">'.$sb->dt('Submit Enquiry').'</ 

a></li> 
</ul> 
</td></tr></table> 
<p style="text-align:right !important"> 
<!--<input name="submit"  style="border:none" type="image" src="./img/ 
en/btn-sendenquiry.gif" title="'.$sb->dt("Send Enquiry").'">--> 
</p> 
</form> 
</p> 
<p><em>'.$sb->dt('*Mandatory fields').'<br /> 
</em></p> 
'; 
$content .= '</div>'; 
echo pageHeader($sb); 
echo pageContent($sb, $content); 
echo pageFooter($sb); 
?>

And here is the code for my verify.php:

<?php 
  require_once('recaptchalib.php'); 
  $privatekey = "6LeiEskSAAAAAHy701oWiFVDXf5YJhRKiRRQgnqH"; 
  $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 
        header("Location: http://www.google.com"); 
  } else { 
    header("Location: contact-complete2.php"); 
  } 
  ?>

As stated in the link, output the $_POST array to check whether all values have been set correctly. Also show output for your REMOTE_ADDR.

As stated in the link, output the $_POST array to check whether all values have been set correctly. Also show output for your REMOTE_ADDR.

I'm sorry. I don't know how to do that (I've barely touched PHP before). I'm not even meant to be working on this website :)

How exactly do I do that?

It shows exactly how to do it in the link you posted, just copy/paste that to your verify.php

OK, done that (didn't know how to show the output of REMOTE_ADDR though). Here's what it gave me back:

_POST: =========
Array
(
[Location] => verify.php
[EnquiryType] => Sales
[FullName] => Joe Bloggs
[Company] => Joe Bloggs Company
[JobTitle] => Top Man
[address_1] => Test Address
[address_2] =>
[address_3] =>
[address_4] => Test City Joe
[Postcode] => JOE BLOG5
[country] => Azores
[Telephone] =>
[Email] => joebloggs@testestestestestestest.com
[Fax] =>
[Website] =>
[Enquiry] => test Joe Bloggs
)

=========

Warning: Cannot modify header information - headers already sent by (output started at /home/electrix/public_html/verify.php:4) in /home/electrix/public_html/verify.php on line 14

What next do you think?

Oh, and here's the output of REMOTE_ADDR (I've blanked a few things out that I'm not sure if you need to see):

Array
(
[UNIQUE_ID] => ThgGYIJOKJHNSjN@NbAAHGAG
[HTTP_HOST] => www.electrix.co.uk
[HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22
[CONTENT_LENGTH] => 301
[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[HTTP_ORIGIN] => http://www.electrix.co.uk
[CONTENT_TYPE] => application/x-www-form-urlencoded
[HTTP_REFERER] => http://www.electrix.co.uk/contact2.php
[HTTP_ACCEPT_LANGUAGE] => en-us
[HTTP_ACCEPT_ENCODING] => gzip, deflate
[HTTP_COOKIE] => PHPSESSID=67986ac64db43f2c75579acb41d1f91c; __utmb=71408455; __utma=71408455.1963067147.1309790052.1319113820.1319450474.69; __utmc=71408455; __utmz=71408455.1319113820.68.19.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=electrix
[HTTP_CONNECTION] => keep-alive
[PATH] => /sbin:/usr/sbin:/bin:/usr/bin
[SERVER_SIGNATURE] =>
[SERVER_SOFTWARE] => Apache
[SERVER_NAME] => www.electrix.co.uk
[SERVER_ADDR] => Blanked out this.
[SERVER_PORT] => 80
[REMOTE_ADDR] => Blanked out this.
[DOCUMENT_ROOT] => /home/electrix/blanked out this
[SERVER_ADMIN] => webmaster@electrix.co.uk
[SCRIPT_FILENAME] => /home/electrix/blanked out this/verify.php
[REMOTE_PORT] => 51565
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => POST
[QUERY_STRING] =>
[REQUEST_URI] => /verify.php
[SCRIPT_NAME] => /verify.php
[PHP_SELF] => /verify.php
[REQUEST_TIME] => 1319451078
[argv] => Array
(
)

[argc] => 0
)

As you can plainly see, [recaptcha_challenge_field] and [recaptcha_response_field] are missing, causing the check to fail. There most be something wrong with the form, it is not posting everything you need.

Right. But how do I get them in there then? I'm sorry. I'm literally a novice with this stuff. If you could explain that it would be amazing?

The form tags are wrapped around the entire table which contains the fields and the captcha. Can't see why it wouldn't see the captcha in the results.

Try to get just the recaptcha working, without all the other stuff. Make sure that works, before you proceed. (I have no access to a php server at work, nor do I have a recaptcha account.)

The recaptcha shows up on the page fine. And it is within the form tags which are wrapped around the table which holds the fields. I can't see why it wouldn't see it.

It can be a dozen of things. Make a page that only outputs the recaptcha, without any js, and make sure that does what it should.

Then it must be something on the page that is interfering, some javascript most likely.

Might post it over in the Javascript forum then :)

pritaeas...

I've finally got it working. It turns out that there was a stray </div> in the code, which when removed fixed the issue.

I now have a second question, but I think it should be an easy one for someone like you...

How do I get the following verification code to redisplay the form (including all the fields filled out) with an error saying "the captcha was entered incorrectly, please try again" when the verification shows that the code has been entered wrong? At the minute I'm just sending incorrect captcha verifications to google.co.uk

<?php require('sbsquared.class.php'); ?>
<?php
  require_once('recaptchalib.php');
  $privatekey = "myprivatekey";
  $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
	header("Location: http://www.google.com");  <--- this is the bit that I want to redisplay the form with fields already filled out.
  } else {
    
	$sb = New SBSquared;

	$name = $_POST['FullName'];
	$post_keys = array_keys($_POST);
	$my_db_string = "<table>";
	$ip_address = $_SERVER['REMOTE_ADDR'];
	foreach($post_keys as $field)
	{
		if($_POST[$field] != "" && $field != "submit_y" && $field != "submit_x" && $field != "submit_x")
		{
		
			$my_db_string .= "<tr><td><b>".$field.":</b></td><td>";
			if($field == "Email")
			{
				$my_db_string .= '<a href = "mailto:'.$_POST['Email'].'">'.$_POST['Email'].'</a>';
			}
			else
			{
				$my_db_string .= $_POST[$field];
			}
			
			$my_db_string .= "</td></tr>";
			
		}
	}
	
	$my_db_string .= "<tr><td><b>IP ADDRESS LOGGED: </b></td><td>".$ip_address."</td></tr>";
	
	$my_db_string .= "</table>";
	
	if(get_magic_quotes_gpc() != 1)
	{
		$my_db_string = addslashes($my_db_string);
		$name = addslashes($name);
	}

	$conn = $sb->openConnection();
	$dts = time();
	$sql = "INSERT INTO `contact_queries` VALUES ('', '$name', '$my_db_string', 'n/a', 0, $dts)";
	$result = mysql_query($sql, $conn) or die(mysql_error());
	
$content = '<div id="main_middle">';

$content .= '<span class="title">'.$sb->dt('Contact').'</span>
<p>'.$sb->dt('Thank you for your enquiry. We will contact you shortly.').'</p>


</div>';

// admin auto email.
		$dts = date("d.m.y h:ia", time());
		$admin_content = "New contact query at $dts";
		$admin_content .= "\n\n--\n\n \r\n\r\n";
		mail("email address", 'NOTIFICATION: new query', $admin_content, 'From: email address');
		$FILE=fopen("./log/auto-contact.txt","a");
		fwrite($FILE, $admin_content);
		fclose($FILE);

echo pageHeader($sb);
echo pageContent($sb, $content);
echo pageFooter($sb);
  }
?>

Instead of header("Location: http://www.google.com"); I would do something like this header("Location: index.php?error=recaptcha"); Assuming index.php is the file you have your form in. Somewhere on your page you can check if ($_GET['error'] == 'recaptcha') and output a custom message in addition to your form.

Cool. And how would you go about refilling the form with the info they've already entered, so they don't have to fill the whole thing out again?

See your other thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.