BotDetect CAPTCHA PHP Code Samples (BotDetect v3.0; deprecated)

The BotDetect PHP Captcha package includes a number of code samples which can help you get started with integrating BotDetect in your PHP websites and configuring various Captcha options.

First Time Here?

Check the BotDetect Developer Crash Course for key integration steps.

Table of Contents

BotDetect PHP CAPTCHA Code Samples Location

After unpacking the BotDetect Captcha library, you can find the PHP Captcha code samples in the samples folder. If you copy the contents of this this folder to a path accessible by your Http server software configured to run PHP (the htdocs folder in Apache installations, Inetpub on IIS, etc.), the index.html will guide you to individual samples.

BotDetect CAPTCHA PHP Integration Code Samples

Integration code samples show how to integrate BotDetect in your PHP websites.


PHP Basic Captcha Code Sample

This code sample shows the most basic source code required to protect a PHP form with BotDetect Captcha and validate the user input.

It can be used as a starting point when you are first learning how to use BotDetect.

Default Source Code Folder Online Source
samples/php_basic_captcha_sample

PHP Form Captcha Code Sample

This code sample shows how to add BotDetect Captcha protection to a typical PHP form.

Captcha validation is integrated with other form fields validation, and only submissions that meet all validation criteria are accepted.

This kind of validation could be used on various types of public forms which accept messages, and are at risk of unwanted automated submissions.

For example, it could be used to ensure bots can't submit anything to a contact form, add guestbook entries, blog post comments or anonymous message board / forum replies.

Default Source Code Folder Online Source
samples/php_form_captcha_sample

PHP Login Form Captcha Code Sample

This code sample shows how to add BotDetect Captcha validation to simple PHP login forms.

To prevent bots from trying to guess the login info by brute force submission of a large number of common values, the visitor first has to prove they are human (by solving the Captcha), and only then is their username and password submission checked against the authentication data store.

Also, if they enter an invalid username + password combination three times, they have to solve the Captcha again. This prevents attempts in which the attacker would first solve the Captcha themselves, and then let a bot brute-force the authentication info.

Default Source Code Folder Online Source
samples/php_login_form_captcha_sample

PHP jQuery Validation Captcha Code Sample

This code sample shows how to integrate BotDetect PHP Captcha validation with jQuery Validation client-side form validation.

Client-side validation is not secure by itself (it can be bypassed trivially by bots that don't execute JavaScript at all), so the sample shows how the protected form action must always be secured by server-side Captcha validation first, and uses client-side validation only to improve the user experience.

Default Source Code Folder Online Source
samples/php_jquery_validation_captcha_sample

PHP Built-In Ajax Captcha Validation Code Sample

This code sample shows how to properly perform Ajax Captcha validation using built-in BotDetect client-side functionality, which doesn't require any 3rd party Ajax frameworks.

It uses the Captcha Form Sample as a starting point, and adds client-side validation of all form fields.

Ajax Captcha validation improves the user experience by reducing Captcha validation response time, giving users much faster feedback about the validation result.

Default Source Code Folder Online Source
samples/captcha_ajax_validation_sample

PHP Multiple CAPTCHAs Code Sample

This code sample shows how to have multiple Captcha-protected pages within the same PHP website.

As long as the Captcha instances have different names ("Captcha1" and "Captcha2" in the sample), they can have completely separate settings and won't interfere with each other's validation.

Even if a user opens the same page in multiple browser tabs, each tab will properly validate the shown Captcha code.

Default Source Code Folder Online Source
samples/php_multiple_captchas_sample

BotDetect CakePHP CAPTCHA Integration Code Samples

Captcha CakePHP integration code samples show how to use BotDetect Captcha protection in your CakePHP applications.

CakePHP Basic BotDetect Captcha Code Sample

This sample illustrates the most basic form of BotDetect PHP Captcha protection in CakePHP MVC applications.

Alongside the Captcha image, the user is provided with an input field to retype the displayed characters. Depending on if the Captcha code entered matches the displayed one or not, a message stating the validation result is shown on the form.

The simple code showing the message in this sample would of course be replaced with useful form processing code in a real world scenario.

Default Source Code Folder Online Source
Part of the BotDetect Captcha CakePHP integration package

CakePHP Form Model Validation BotDetect Captcha Code Sample

This sample shows how to integrate BotDetect PHP Captcha validation and CakePHP data validation functionality. It uses Cake's FormHelper and Model validation, which provide a lot of out-the-box functionality when used together.

The sample is based around a contact form which sends email if the user input is considered valid – a likely real world scenario for Captcha protection integration.

To keep the sample brief, it doesn't use a database backend. Still, this type of Model integration is ideal for protecting database-driven forms that take advantage of the CakePHP Model infrastructure.

Default Source Code Folder Online Source
Part of the BotDetect Captcha CakePHP integration package

CakePHP Auth Captcha Code Example

This example demonstrates how to integrate BotDetect PHP Captcha into login and register forms in a scenario where CakePHP Auth Component is used to authenticate users to your application.

The example is based around a login form and a registration form which authenticate users and add new users if the user input is considered valid – a likely real world scenario for Captcha protection integration.

Default Source Code Folder Online Source
Part of the BotDetect Captcha CakePHP integration package

BotDetect CAPTCHA CodeIgniter Integration Code Samples

Captcha CodeIgniter integration code samples show how to use BotDetect Captcha protection in your CodeIgniter applications.

CodeIgniter Basic BotDetect Captcha Code Sample

This sample illustrates the most basic form of BotDetect PHP Captcha protection in CodeIgniter MVC applications.

Alongside the Captcha image, the user is provided with an input field to retype the displayed characters. Depending on if the Captcha code entered matches the displayed one or not, a message stating the validation result is shown on the form.

The simple code showing the message in this sample would of course be replaced with useful form processing code in a real world scenario.

Default Source Code Folder Online Source
Part of the BotDetect Captcha CodeIgniter integration package

CodeIgniter Form Validation BotDetect Captcha Code Sample

This sample shows how to integrate BotDetect PHP Captcha validation and CodeIgniter form validation functionality. It uses the CodeIgniter Form helper and the Form Validation library.

The brief sample is based around a contact form which sends email if the user input is considered valid – a likely real world scenario for Captcha protection integration.

Default Source Code Folder Online Source
Part of the BotDetect Captcha CodeIgniter integration package

CodeIgniter Bit Auth Captcha Code Example

This example demonstrates how to integrate BotDetect PHP Captcha into login and register forms in a scenario where Bit Auth library is used to authenticate users to your application.

The example is based around a login form and a add new form which authenticate users and add new users if the user input is considered valid – a likely real world scenario for Captcha protection integration.

Default Source Code Folder Online Source
Part of the BotDetect Captcha CodeIgniter integration package

CodeIgniter Ion Auth Captcha Code Example

This example demonstrates how to integrate BotDetect PHP Captcha into login and forgot password forms in a scenario where Ion Auth library is used to authenticate users to your application.

The example is based around a login form and a forget password form which authenticate users and sends email if the user input is considered valid – a likely real world scenario for Captcha protection integration.

Default Source Code Folder Online Source
Part of the BotDetect Captcha CodeIgniter integration package

CodeIgniter Tank Auth Captcha Code Example

This example demonstrates how to integrate BotDetect PHP Captcha into login, register, forget password and send again forms in a scenario where Tank Auth library is used to authenticate users to your application.

The example is based around a login, register, forget password and send again forms which authenticate users, add new users and sends email if the user input is considered valid – a likely real world scenario for Captcha protection integration.

Default Source Code Folder Online Source
Part of the BotDetect Captcha CodeIgniter integration package

BotDetect CAPTCHA Laravel Integration Code Examples

Captcha Laravel integration code examples show how to use BotDetect Captcha protection in your Laravel applications.

Laravel Basic BotDetect Captcha Code Example

This example illustrates the most basic form of BotDetect PHP Captcha protection in Laravel MVC applications.

Alongside the Captcha image, the user is provided with an input field to retype the displayed characters. Depending on if the Captcha code entered matches the displayed one or not, a message stating the validation result is shown on the form.

The simple code showing the message in this example would of course be replaced with useful form processing code in a real world scenario.

Default Source Code Folder Online Source
Part of the BotDetect Captcha Laravel integration package

Laravel Form Validation BotDetect Captcha Code Example

This example shows how to integrate BotDetect PHP Captcha validation and Laravel validation functionality. It uses Laravel's Forms & HTML and Validation class.

The example is based around a contact form which sends email if the user input is considered valid – a likely real world scenario for Captcha protection integration.

Default Source Code Folder Online Source
Part of the BotDetect Captcha Laravel integration package

Laravel Auth Captcha Code Example

This example demonstrates how to integrate BotDetect PHP Captcha into login and register forms in a scenario where Laravel Auth is used to authenticate users to your application.

This example also uses Forms & HTML to generate a form, Validation class to validate input fields data, and Blade Templating.

The example is based around a login form and a registration form which authenticate users and add new users if the user input is considered valid – a likely real world scenario for Captcha protection integration.

Default Source Code Folder Online Source
Part of the BotDetect Captcha Laravel integration package

BotDetect PHP CAPTCHA Configuration Code Samples

Captcha options code samples show how to use particular BotDetect Captcha options in your PHP websites.

PHP Captcha Code Filtering Code Sample

This code sample shows how to use the new Captcha code filtering functionality added in BotDetect 3.0.

You can define rules about character sequences you want to avoid using in randomly generated Captcha codes and pass them to the Captcha library.

The botdetect/CaptchaConfig.php file defines a simplified custom character set (using only 'A', 'B', 'C' and 'D'), which helps make the code filtering easier to track in action.

The $LBD_CaptchaConfig->BannedSequences setting in the same file shows how to create your own banned sequence definitions to suit your application needs. The following sequences will never appear in Captcha codes generated in the sample: D, AA, BB, CC, ABC, BCA, CAB.

Default Source Code Folder Online Source
samples/captcha_code_filtering_sample

PHP Captcha Customization Code Sample

This code sample shows how to customize BotDetect Captcha behavior and appearance.

BotDetect 3.0 allows user-defined customization of many Captcha options through a special botdetect/CaptchaConfig.php configuration file; some customizations also require page source changes.

The botdetect/CaptchaConfig.php file used in this code sample contains detailed descriptions and explanations of the many customizable options.

You can then use chosen customization options to configure BotDetect to precisely match your application requirements.

Default Source Code Folder Online Source
samples/captcha_customization_sample

PHP Captcha Randomization Code Sample

This code sample shows how to easily randomize various Captcha control parameters, beyond the basic image and sound style randomization used by default.

Randomly using different Captcha image and sound generation algorithms and other parameter values (such as code length and style) can significantly improve the Captcha security.

This is the recommended approach to Captcha property setting, since it takes full advantage of the 60 Captcha image and 10 Captcha sound generation algorithms shipped with BotDetect, as well as built-in randomization features.

Default Source Code Folder Online Source
samples/captcha_randomization_sample

Please Note

The information on this page is out of date and applies to a deprecated version of BotDetect™ CAPTCHA (v3.0).

An up-to-date equivalent page for the latest BotDetect Captcha release (v4) is BotDetect v4 Captcha documentation index.

General information about the major improvements in the current BotDetect release can be found at the What's New in BotDetect v4.0 page.