It is very easy to use CAPTCHA. Simply add the following code in the desired place in your frontend template:
{$wa->captcha()}
And verify the entered code within your PHP code:
if (wa()->getCaptcha()->isValid()) { // CAPTCHA code entered correctly } else { // entered incorrectly }
Smarty tag {$wa->captcha}
returns HTML code to display a CAPTCHA, which contains an image (img
), a text field (input
)
to enter the code, and a link to reload the CAPTCHA image:
<div class="wa-captcha"> <p> <img class="wa-captcha-img" src="{$captcha_url}" alt="CAPTCHA" title="Refresh CAPTCHA"> <strong>→</strong> <input type="text" name="captcha" class="wa-captcha-input" autocomplete="off"> </p> <p> <a class="wa-captcha-refresh">Refresh CAPTCHA</a> </p> <script> // ... JavaScript code to refresh the CAPTCHA </script> </div>
Its design is easily customizable by means of CSS, because all key elements' CSS class names have the wa-captcha
prefix.
If, instead of the above shown code you need to display different HTML code for your CAPTCHA, then this can be achieved with the help of method
{$wa->captchaUrl()}
, which returns only the URL of the CAPTCHA image. The default image dimensions are 120x40px. Example:
<img src="{$wa->captchaUrl()}"> <input type="text" name="code" />
If a value different from captcha
is specified for the name
attribute of the input
element, where CAPTCHA code is entered,
then the server-side verification shall be carried out as shown below:
$code = waRequest::post('code'); wa()->getCaptcha()->isValid($code);
Restrictions: you cannot display two CAPTCHA images on one page. The code displayed on the CAPTCHA image is stored in the current PHP session.
Overriding the standard CAPTCHA
At the application level you can specify the PHP class used for CAPTCHA handling and set up the parameters of the standard CAPTCHA (image dimensions, content, etc.).
By default class waCaptcha
is used. Below are shown the settings used in waCaptcha
:
'chars' => 'abdefhknrqstxyz23456789', 'width' => 120, // px 'height' => 40, // px 'font_size' => 25, // pt
To override these settings of waCaptcha
for your application, create file
wa-apps/APP_ID/lib/config/factories.php
with the following contents:
<?php return array( 'captcha' => array( 'waCaptcha' , array( 'width' => 200, 'height' => 70, 'font_size' => 40, ), ), );
Such overriding will resize the CAPTCHA in your application to 200x70 px and will change its font size to 40pt.
To write your own CAPTCHA class, it should inherit base class waAbstractCaptcha
.
The following methods must be implemented in the custom class:
public function isValid($code = null) // must return true, if the code is correct, or false otherwise public function getHtml() // must return complete HTML code of the CAPTCHA (including image tag 'img' and text field tag 'input' for entering CAPTCHA code) public function display() // must return CAPTCHA image
Thus, the overriding class must have the following structure:
<?php class myCaptcha extends waAbstractCaptcha { public function isValid($code = null) { } public function getHtml() { } public function display() { } }
As a working example you can consider the implementation of the standard class waCaptcha
.
When the necessary code has been written in your CAPTCHA handling class, specify the class name in file factories.php
as shown below:
return array( 'captcha' => 'myCaptcha', );