reCAPTCHA as MySQL code

23 views
Skip to first unread message

metazai

unread,
Mar 27, 2009, 9:20:19 AM3/27/09
to reCAPTCHA
Greetings . . . I've got a custom-built CMS written in procedural PHP,
and one of my clients wishes to use reCAPTCHA. I'm having trouble
making it work the way he wants it to, however, and I'd like some
advice on troubleshooting and/or feasibility of this setup.

My CMS has a "script manager" which allows the client to have a
library of scripts, forms, and includes that he can sprinkle
throughout his pages. I currently have the code of the short little
form that utilizes the reCAPTCHA saved in this script manager which
utilizes a MySQL database to contain the scripts. When a page that
needs it opens, it pulls the correct script from the database:

-------------------------------------------------------------

<?php include ("includes/sqlconn.php");$scriptrow = mysql_fetch_object
(mysql_query("SELECT * FROM scripts WHERE name='captcha_form'"));echo
($scriptrow->content); ?>

-------------------------------------------------------------

, which in this case looks like this (I've removed the public key,
obviously):

-------------------------------------------------------------

<div style="padding:0px 0px 0px 20px;z-index:9000;"><form
action="includes/captcha_form_processor.php" method="post"><span
style="font-size:10px;">
<img src="images/contactus_header.gif" border="0" /><br />
Name<br />
<input id="Name" name="Name" type="text" size="26" /><br />
Email (required)<br />
<input id="Email" name="Email" type="text"
size="26" /><br />
Phone<br />
<input id="Phone" name="Phone" type="text" size="26" /><br />
Details<br />
<textarea id="Comments" name="Comments" cols="20"></textarea><br /
>
Please enter the following:<br />
<?php
require_once('recaptchalib.php');
$publickey = "..."; echo recaptcha_get_html($publickey);
?>
<br />
<input id="txtCaptcha" type="text" size="26" name="txtCaptcha"
value="text is case sensitive!" /><br /><br />
<input src="images/submit_button.gif" type="image" /></span>
<div id="result"> </div>
</form></div>

-------------------------------------------------------------

Everything shows up except the captcha. I've confirmed that
recaptchalib.php is where is should be, and it appears to be storing
correctly in the database (I've looked at it from the backend). Any
thoughts on whether this code has problems when pulled from a database?

Lesley Binks

unread,
Mar 30, 2009, 4:58:37 AM3/30/09
to reca...@googlegroups.com
2009/3/27 metazai <met...@gmail.com>:

1. Have you tested the recaptcha code actually works?
This code does at can be viewed at
http://herlug.org.uk/test/recaptcha/php/recaptchatest.php

<html>
<body>
<form action="" method="post">
<?php

require_once('recaptchalib.php');


// Get a key from http://recaptcha.net/api/getkey
$publickey = '';
$privatekey = '';

# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;

# was there a reCAPTCHA response?
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) {
echo "You got it!";
} else {
# set the error code so that we can display it
$error = $resp->error;
}
}
echo recaptcha_get_html($publickey, $error);
?>
<br/>
<input type="submit" value="submit" />
</form>
</body>
</html>

Note you don't need a separate input field for the recaptcha - it is
already in the recaptcha.
You also need to use the relevant recaptcha form variables,
recaptcha_challenge_field and recaptcha_response_field
Lastly you can't have two recaptchas on one form unless you use the AJAX method.

Regards

Reply all
Reply to author
Forward
0 new messages