BotDetect CAPTCHA PHP Code Samples & Demos
This is an overview of code samples and demos included with the BotDetect 3 PHP CAPTCHA Library.
BotDetect CAPTCHA Demos
Demo forms demonstrate various BotDetect Captcha features and options.
BotDetect Captcha Features Demo
This demo allows you to easily experiment with various BotDetect parameters and their combinations, so you can see how powerful and customizable BotDetect Captcha is.
[back to top]
BotDetect PHP CAPTCHA Integration Samples
These code samples show how to integrate BotDetect in your PHP applications.
Php Basic Captcha Code Sample
This code sample shows the most basic source code required to protect an 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.
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.
If the Captcha is sucessfully solved but other field validation fails, the Captcha is hidden since the users have already proven they are human.
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.
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.
Php jQuery Validation Captcha Code Sample
This code sample shows how to integrate BotDetect PHP Captcha validation with jQuery Validation client-side form validation.
It uses the Captcha Form Sample as a starting point, and adds client-side jQuery Validation rules for all form fields.
Client-side validation is not secure by itself (it can be bypassed trivially), so the sample also shows how the protected form action must always be secured by server-side Captcha validation first, and use client-side validation only to improve the user experience.
Php jQuery Ajax Contact Form Captcha Code Sample
This code sample demonstrates the use of BotDetect Captcha in a scenario using jQuery and AJAX to validate individual form fields against a PHP server backend. The approach in this scenario is useful in situations where duplication of server-side validation routines on the client side is impractical.
To make the sample brief and concrete, it is implemented as an AJAX contact form with Captcha protection.
The sample shows proper steps to be taken when validating the Captcha in various ways this scenario provides: be it partial validation or complete form validation and submission using AJAX; or server side validation only when Javascript is disabled.
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.
Client-side validation is not secure by itself (it can be bypassed trivially), so the sample also shows how the protected form action must always be secured by server-side Captcha validation as well.
In case of any Ajax errors or timeouts, the sample simply falls back to full form posting and server-side Captcha validation.
Php Multiple Captchas 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.
[back to top]
BotDetect CakePHP Captcha Integration Samples
These code samples show how to integrate BotDetect in your CakePHP applications. To keep the basic BotDetect Captcha library download reasonably small, CakePHP Captcha integration code and samples are a separate download available from the BotDetect CakePHP Captcha page .
CakePhp Basic BotDetect Captcha Sample
This code 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.
CakePhp Form Model Validation BotDetect Captcha Sample
This code 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 .
[back to top]
BotDetect CodeIgniter Captcha Integration Samples
These code samples show how to integrate BotDetect in your CodeIgniter applications. To keep the basic BotDetect Captcha library download reasonably small, CodeIgniter Captcha integration code and samples are a separate download available from the BotDetect CodeIgniter Captcha page .
CodeIgniter Basic BotDetect Captcha Sample
This code 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.
CodeIgniter Form Validation BotDetect Captcha Sample
This code 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.
[back to top]
BotDetect PHP CAPTCHA Configuration Samples
Captcha configuration code samples show how to use particular BotDetect Captcha options in your PHP applications.
Php Captcha Code Filtering 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 simply 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
.
Php Captcha Customization 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 CaptchaConfig.php
configuration file; some customizations also require page source changes.
Shown customizations include: Captcha image color scheme, sound & reload icons and their tooltips, custom client-side handlers for BotDetect actions such as sound playing and Captcha reloading (resulting in simple message boxes on icon clicks in this sample), ...
The 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.
Php Captcha Randomization Sample
This code sample shows how to easily randomize various Captcha library 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.
Setting Captcha
control properties in the CaptchaConfig.php
file also reduces the amount of Session state used by the library.
[back to top]