Howto do a Captcha in Symfony – with Plugins
May 3, 2007
There has quite some time passed since i blogged about how to do a captcha with the JpGraph library.
Since then, two new plugins have shown up in order to ease the captcha integration in symfony projects.
The sfCaptchaPlugin uses the JpGraph library while the sfCryptographpPlugin relies on the cryptographp library.
Both plugins have installation instructions in their wiki-pages, but there are small things that might confuse new users, so i decided to write a howto for both of them.
sfCaptchaPlugin
First, install the plugin:
symfony plugin-install http://plugins.symfony-project.com/sfCaptchaPlugin
and enable the module in the settings.yml file (e.g. apps/frontend/config/settings.yml):
all: .settings: enabled_modules: [default, sfCaptcha]
The sfCaptchaPlugin comes with the digits-only version of the JpGraph library (which is much more readable than the alphanumeric version), but in order to use it you need to open the plugins/sfCaptchaPlugin/lib/Captcha.class.php file and change:
require_once 'jpgraph/jpgraph_antispam.php';
to:
require_once 'jpgraph_antispam-digits.class.php';
Now we need to generate the captcha and save the key in the session. Add the following lines to the function which displays your form:
$g = new Captcha(); $this->getUser()->setAttribute('captcha', $g->generate());
and in the template, add:
<?php use_helper('Validation'); ?> <img src="<?php echo url_for('sfCaptcha/index'); ?>" alt="captcha" /> <?php echo form_error('captcha'); ?> <?php echo input_tag('captcha'); ?>
To validate the input, create a validation file in your modules validate folder and add:
fields: captcha: required: msg: Please enter the numbers in the captcha image captchaValidator: error: Incorrect code
Finally, clear your cache with symfony cc and the captcha should be ready.
sfCryptographpPlugin
Install the plugin with:
symfony plugin-install http://plugins.symfony-project.com/sfCryptographpPlugin
and enable the module in the settings.yml file (e.g. apps/frontend/config/settings.yml):
all: .settings: enabled_modules: [default, cryptographp]
Open the template file which contains your form, and add:
<?php use_helper('Cryptographp', 'Validation'); ?> <?php echo cryptographp_picture(); ?> <?php echo cryptographp_reload(); ?> <?php echo form_error('captcha'); ?> <?php echo input_tag('captcha'); ?>
To validate the input, create a validation file in your modules validate folder and add:
fields: captcha: required: msg: security code required sfCryptographpValidator: code_error: security code incorrect
Finally, clear your cache with symfony cc and you should be ready to go.
Unlike JpGraph, the cryptohraphp lib comes with a variety of config options, you can take a look at the documentation and the plugins/sfCryptographpPlugin/lib/crypt/config.inc.php file.
For example, if you want to modify the width of the captcha image, open your applications app.yml file and add:
all: cryptographp: width: 300
May 17, 2007 at 4:53 pm
Excellent cookbook dude! Might I add that the php_gd2.dll extension of PHP needs to be enabled for the JPGraph library to run. I tried downloading the sfJPGraph plugin but it didn’t work so I downloaded the JPGraph library (v2.2) instead and installed it in my project’s lib folder. The library worked when I expanded the files into a folder like so: C:\home\vhosts\testbed.wlprojects.com\lib\jpgraph\
Again, kudos to you good sir!
May 17, 2007 at 6:02 pm
Thanks a lot !!!
You kinda save my life ;)
I was making a mistake with the file settings.yml where I kept writing :
enabled_modules: [default, sfCryptographpPlugin]
and wonders why on earth it doesn’t work :s
July 26, 2007 at 7:45 am
i’ve got a question, why the image in captcha doesn’t show?where will i put my images in order to be called?
thanks
January 21, 2008 at 11:43 am
Since I didn’t get sfCaptchaPlugin working after many tries the sfCryptographpPlugin worked at once. Thanks for your description.
February 4, 2008 at 8:29 pm
Hi,
found your reCaptchaPlugin for symfony1.0.10 and was wondering, if there will be an update for symfony1.1 with the new formHandling.
Greetings
Susan
February 4, 2008 at 8:36 pm
Hi Susan,
As soon as Symfony 1.1 hits beta status i’m going to work on it.
PS.: Further updates are going to be on my new blog http://arthurkoziel.com
Arthur.
April 21, 2008 at 4:15 pm
Thanks for this post! Very helpful!
May 12, 2008 at 8:34 pm
This tutorial is wrong re; jpGraph.
It is not bundled with sfCaptchaPlugin.
July 6, 2008 at 9:27 pm
symfony1.1 is now better than beta. I started to think about a solution for sfReCaptcha. But maybe you are already working on this?
Greetings
Susan
July 6, 2008 at 9:39 pm
Hey Susan, I indeed started to work on it but it’s still not finished. The documentation on sfForm in sf1.1 ist still very sparse so it may still take some time.
July 7, 2008 at 6:07 am
yeah. I can imaging :-)
I am glad, that we have now a sfForm-documentation. But it is still not enough. You are right. I try to be patient ;-)
July 10, 2008 at 5:21 pm
done. http://arthurkoziel.com/2008/07/10/sfrecaptchaplugin-for-symfony-11/
April 30, 2009 at 3:11 am
problem: Image doesn’t appear.
solution:
1.-make sure you have gd libraries enabled.
2.-if u’re using sfguardplugin, then disable security for this action.
My first test was:
in the file “project_name/apps/app_name/config/security.yml”
change from:
default:
is_secure: on
to
default:
is_secure: off
and it worked..so I disable the security in the action, and validate every input in the form.
Hope this help. C’ya
May 23, 2009 at 3:34 pm
Hi , I followed the eact same configuration steps provided by you.
I was really in a serious fix gettimg the image to load and show up on the template…
I would like to further add that I have configured security to
default:
is_secure: off
in /apps/frontend/config/security.yml file
any immediate help would be appreciated..
thanks a million in advance :)
May 25, 2009 at 6:21 am
Hi,
I’ve been trying to figure out one thing,
in my settings.yml file, I have enabled sfCaptcha using:
enabled_modules: [ default, sfCaptcha ]
but I’m still unable to fix the exception that says,
The module “sfCaptcha” is not enabled.
the steps given above are helping me set the session properly, but I am unable to get the image working
I browsed through the plugin and found out that it is actions.class.php in the plugin using the index action
which plots the image
so I even created a view.yml with http_metas as image/png
and when I directly access indexSuccess.php through the url to see if the image is getting generated,
it throws an exception
The module “sfCaptcha” is not enabled.
can you please get me out of this mess…
September 1, 2009 at 2:52 pm
@nitin
Had the same trouble with sf1.1, check this forum post: http://forum.symfony-project.org/index.php/m/83055/
Hope it helps