Using captcha in extensions

This page is not translated yet. Showing original version of page. You can sign up and help with translating.

When developing an extension, it is often necessary to display some form to unregistered users: a feedback form, "order a call", ask a question to support, etc. Such forms can be filled out and sent to the server by spam bots, thereby filling the database with garbage. A captcha is a very effective way to protect against such bots.

It makes no sense to require registered users to fill out a captcha because bots cannot log in to the site and "spam" all forms. Therefore, you need to display the captcha only in those forms that can be filled by the guests.

Using a captcha in your extensions is pretty simple.

To display a captcha in any form:

Add such code to the your extension's controller:

// Initialize the template engine
$t = new XTemplate($pathToTemlateFile);

// ... some code of your extension ...

// Check if user is not logged in and at least one captcha is installed on the site:
if (Cot::$usr['id'] === 0 && !empty($cot_captcha)) {
	// Output captcha to the template
	$t->assign(cot_generateCaptchaTags(null, 'rverify', 'FEEDBACK_FORM_'));
}

Add tags to the template for captcha output:

<!-- IF {FEEDBACK_FORM_VERIFY_IMG} -->
<div>
	{FEEDBACK_FORM_VERIFY_IMG} {FEEDBACK_FORM_VERIFY_INPUT}
</div>	
<!-- ENDIF -->

And finally, when processing the data sent by the user, you need to check whether the captcha is filled in correctly:

if (Cot::$usr['id'] === 0 && !empty($cot_captcha)) {
	$rverify = cot_import('rverify', 'P', 'TXT');
	if (!cot_captcha_validate($rverify)) {
		// The captcha is not filled correctly
		cot_error('captcha_verification_failed', 'rverify');
	}
}

Henüz yorum yapılmamış
Sadece kayıtlı kullanıcılar yeni yorum yapabilir.