Home Communication board WiKi Get Quote

Embed form captcha

If you have inserted a form to your page in Drupal software, it's possible to add Drupal captcha to it.

First, you should download and install "captcha" module from Drupal site. Also you need the ability to include the php code to that page.

Then you can modify your page content in the following way. The captcha code should be inserted on the very top of the page:

<?php
module_load_include('inc', 'captcha');
$captcha_sid = $_REQUEST['captcha_sid'];
if (!$captcha_sid) 
  $captcha_sid = _captcha_generate_captcha_session('myform');
else 
  $solution = db_result(db_query('SELECT solution FROM {captcha_sessions} WHERE csid = %d AND status = %d', $captcha_sid, CAPTCHA_STATUS_UNSOLVED));

$captcha = module_invoke('image_captcha', 'captcha', 'generate', 'Image', $captcha_sid);
_captcha_update_captcha_session($captcha_sid, $captcha['solution']);
?>

This code will prepare the captcha for displaying or checking. It should be inserted before any other your functions. Any php code that would work with the form data should be inserted below this code. Here the captcha can be checked in the following way:

<?php
 if (captcha_validate_case_insensitive_ignore_spaces($solution, $_POST['antispam'])) {
      db_query("UPDATE {captcha_sessions} SET status=%d, attempts=attempts+1 WHERE csid=%d", CAPTCHA_STATUS_SOLVED, $captcha_sid);
# here the form actions
 else {
# here the error
 }
?>

this code will compare the saved captcha value with the one received from the user.

And you can show the captcha image in any place of your form using the following simple code:

<input type='text' name='antispam' id='antispam' maxlength='8' /><br>
<input type="hidden" name="captcha_sid" value="<?php echo $captcha_sid; ?>" />
<?php echo $captcha['form']['captcha_image']['#value']; ?>

This code will display the text box for the customer input, the hidden value with the capture #id and the capture image itself.

 
Home About us Privacy statement Terms & Conditions Refund policy © 2007–2024 ArsCommunity