Skip to content

[EN] Add a captcha (addons)

Thuban edited this page Mar 20, 2018 · 2 revisions

To use a CAPTCHA generated by blogotext in a form, follow these instructions

How blogotext CAPTCHa works ?

When inc/boot.php file is required, a random operation is generated (the sum of two integers) The result is saved as a token : the hash of the result with the visitor user agent. When the form is submitted, these are send :

  • The result entered by the visitor
  • The hash pre-computed (hidden)

To check a captcha, compare the token with a new hash computed from user input.

Using CAPTCHA

Sending form

  1. require inc/boot.php in your code. It's already prepared for addons.
  2. Show the two integers to add, accessible with these variables :
  • $GLOBALS['captcha']['x']
  • $GLOBALS['captcha']['y']
  1. Turn one of those integers in full letters. Use the function en_lettres($GLOBALS['captcha']['y'])
  2. Add and hide the token in the form with the function hidden_input('your_input_token', $GLOBALS['captcha']['hash'])

you can use the function captcha_form() to insert the above code easily.

check the Captcha

  1. Get the result entered by the visitor : $datas['captcha'] = filter_input(INPUT_POST, 'your_input_captcha', FILTER_SANITIZE_SPECIAL_CHARS);
  2. get the token : datas['token'] = filter_input(INPUT_POST, 'your_input_token', FILTER_SANITIZE_SPECIAL_CHARS);
  3. Compute the hash and compare with the token :
// user agent
$ua = (isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : '';
if ($datas['token'] != sha1($ua.$datas['captcha'])) {
// error ! 
}

You can use the function captcha_check($token, $captcha) which returns true if the captcha is valid instead of the whole above code.