sfCaptcha plugin
The sfCaptchaPlugin is a symfony plugin that provides captcha functionality based on JpGraph library.
It gives you the lib with Captcha class and the module to secure your symfony forms in a minute with a good captcha.
Installation
Install the plugin
symfony plugin-install http://plugins.symfony-project.com/sfCaptchaPlugin
Enable one or more modules in your settings.yml (optional)
- sfCaptcha
all:
.settings:
enabled_modules: [default, sfCaptcha]
Put JpGraph? core and antispam extension into plugin's /lib folder or modify the following line in Captcha.class.php so that it points to your jpgraph install:
require_once 'jpgraph/jpgraph_antispam.php';
Clear you cache
symfony cc
Secure your form
To secure a symfony form:
Put the following lines inside of your form
<img src="<?php echo url_for('@sf_captcha'); ?>" />
<?php echo input_tag('captcha'); ?>
Put the following code into your form's validator yml file:
methods: [post] # This is the default setting
validators:
captchaValidator:
class: captchaValidator
param:
error: You should specify valid Turing number
fields:
captcha:
required:
msg: You should specify Turing number
captchaValidator:
Put the following code into the action which is for the form where captcha is displayed:
$g = new Captcha();
$this->getUser()->setAttribute('captcha', $g->generate());
You're done. Now, if you try to access a secured form, you will be asked to type in captcha.
Configuration options (from 1.0.2)
Application-wide parameters : alphabet and length
You can change the alphabet and length used by the plugin, in you app.yml file. Default alphabet is "0123456789" (digits) and default length is 4 (four). Be careful avoiding confusing characters (i.e. avoid using "i", "l", and "1" in the same alphabet)
all:
captcha:
alphabet: abcdefghjkmnptwxyz234678
length: 6
Error messages
You can also change the default error message, so you don't have to specify this each time you use the captcha. This can be useful if you use the captcha several times and don't want to repeat yourself in config files.
- Error message can be specified in app.yml :
all:
captcha:
error : You really should try to write down the word you read
- If you do so you don't have to specify the "error: …" parameter in your validator yml file.
- You still can override the error message as usual in the validator yml file.
- You even can not specify any message at all, then the default message "You should specify valid Turing number" will be raised.
Customization Help
Customize captcha generation
If you want to radically customize your captcha generation function then modify the generate() function in lib/Captcha.class.php.
- The default function generates a random captcha from the alphabet given in the validator yaml.
- The default function function saves iData in an sfUser attribute.
- generate() must return iData which is the created image bit-stream.
Clean routing
Add the following routing rules to routing.yml
sf_captcha:
url: /captcha
param: { module: sfCaptcha, action: index }
If you have sfGuard installed and it is allowed to register routes, then the route above will be automatically prepended to your routing table.
Stop browser from caching captcha image
Users may see the same captcha image even after a page refresh because their browser is caching it. To prevent this, add a random string to the end of your image call.
First, in routing.yml modify sf_captcha to read like this:
sf_captcha:
url: /captcha/:key
param: { module: sfCaptcha, action: index, key: failed }
Next in your template, append a random key variable to the captcha image call.
<?php if($sf_user->getAttribute('captcha')): ?>
<img src="<?php echo url_for('@sf_captcha?key='.time()); ?>" />
<?php echo input_tag('captcha'); ?>
<?php else: ?>
There was an error validating your session. Captcha could not be created. <br />
Please reload this page or contact technical support.
<?php endif; ?>
TODO
Attachments
- sfCaptchaPlugin-0.1.0.tgz (2.9 kB) - added by Voznyak.Nazar on 04/24/07 11:37:59.
- sfCaptchaPlugin-1.0.0.tgz (3.1 kB) - added by Voznyak.Nazar on 04/24/07 12:16:44.
- sfCaptchaPlugin-1.0.1.tgz (3.2 kB) - added by Voznyak.Nazar on 04/24/07 16:24:15.
- sfCaptchaPlugin-1.0.2.tgz (2.7 kB) - added by naholyr on 07/14/07 15:51:06.
- sfCaptchaPlugin-1.0.3.tgz (3.3 kB) -
added the package.xml file
, added by Mickael.Kurmann on 07/16/07 14:52:24. - sfCaptchaPlugin-1.0.4.tgz (3.3 kB) -
added fix to remove zero from captcha alphabet as it does not fit newer JpGraph? version
, added by Voznyak.Nazar on 10/24/07 11:49:15.