Views: 3556
Last Modified: 26.10.2021

You can connect CAPTCHA as follows:

Connecting CAPTCHA in module settings

CAPTCHA are connected directly in module settings. For example, the Use CAPTCHA option in the Main/kernel module allows using CAPTCHA when registering new users:

Connecting CAPTCHA in custom scripts, modules or components

First, create CAPTCHA to be used :

01      <?include_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/classes/general/captcha.php");
02	$cpt = new CCaptcha();
03	$captchaPass = COption::GetOptionString("main", "captcha_password", "");
04	if(strlen($captchaPass) <= 0)
05	{
06	    $captchaPass = randString(10);
07	    COption::SetOptionString("main", "captcha_password", $captchaPass);
08	}
09	$cpt->SetCodeCrypt($captchaPass);
10	?>

Next, print three elements inside the form: hidden field with generated code, field to enter captcha by the user and the image itself.

<input name="captcha_code" value="<?=htmlspecialchars($cpt->GetCodeCrypt());?>" type="hidden">
<input id="captcha_word" name="captcha_word" type="text">
<img src="/bitrix/tools/captcha.php?captcha_code=<?=htmlspecialchars($cpt->GetCodeCrypt());?>">

Add a check in the script used to submit the form:

01      <?
02      if(!$APPLICATION->CaptchaCheckCode($_POST["captcha_word"], $_POST["captcha_code"]))
03	{
04	    // Incorrect value
05	}
06	else
07	{
08	    // Correct value
09	}
10	?>

0


Courses developed by Bitrix24