Howto do a Captcha in symfony

March 15, 2007

There’s already a page about this in the symfony-wiki, but i thought i would modify it bit and explain it more detailed.

I will be using the JpGraph class for this because it has a integrated Captcha feature. First tests showed that the usual Captcha with characters and numbers was very hard to pass (really, i tried it like 20 times and didn’t pass it once) so i used only numbers. Also i won’t be using the sfJpGraphPlugin since it lacks the functionality to generate a AntiSpam instance.

First, go to the JpGraph website and download the current version (2.1.4 in my case) and extract it to a location of your choice. Put the jpgraph_antispam-digits.php file into the lib/ folder of your symfony-project and rename it to jpgraph_antispam-digits.class.php. Don’t forget to clear the cache after this (symfony cc).

Create a new action and paste the following code into it:

  public function executeCaptchaImage()
    // Generate a random string and save it in the session
    $randomChars = myTools::generateRandomCaptchaKey(5);
    $this->setFlash('captchaChars', $randomChars);

    // Create a new Captcha with your random string
    $antispam = new AntiSpam($randomChars);

    return $antispam->Stroke();

The generateRandomCaptchaKeyPlease-method is up to you, but please note to use only numbers and that generally the letter ‘O’ and the number ‘0’ are not allowed in the JpGraph-Captcha since they are too alike and can be mistaken for each other (yes, even if you use the “digits”-version).

Now we should set the content-type of the action since we want to output an image and also disable the layout. Open the view.yml in your modules config/ directory and paste the following code into it:

  has_layout: off
    content-type: image/jpeg


Finally, you can print the image in your template:

<img src="<?php echo url_for('upload/captchaImage'); ?>" alt="captcha" />

To validate the captcha, just create a validateXXX-method and simply compare the values:

public function validateIndex()
    $captchaChars = $this->getFlash('captchaChars');
    $formChars = $this->getRequestParameter('body');

    if ($captchaChars != $formChars)
      $this->getRequest()->setError('body', 'wrong');

      return false;

    return true;

In your main-action just process as usual:

  public function executeIndex()
    if ($this->getRequest()->getMethod() == sfRequest::POST) {
      // process form

That’s it, now you should have a fully functional captcha image.


12 Responses to “Howto do a Captcha in symfony”

  1. Morr Says:

    I followed your instructions but I got

    Fatal error: Class ‘myTools’ not found in /home/username/public_html/sf/apps/myapp/modules/mymodule/actions/actions.class.php on line 37

    And I can’t find the class ‘myTools’ anywhere in the JpGraph package, or within symfony…. any idea?

  2. symfoniac Says:

    Sure, as i said the myTools::generateRandomCaptchaKey() method is up to you.

    Just make a new class named myTools with a generateRandomCaptchaKey method.

  3. Neil Mendoza Says:


    This did not work with jpgraph_antispam-digits.php but worked fine with jpgraph_antispam.php

    If you are lazy, you can also find a random string generator in the code here:

  4. renji Says:

    i’ve got a question, why the image in captcha doesn’t show?where will i put my images in order to be called?

  5. wolv Says:

    Hi, im use all how to and the wiki instructions but dont show the image. Really not execute nothing. Please need help

  6. andres Says:

    To anyone who is having problems:
    first check that your jpgraph can be load by the Captcha.class.php script I put the jpgraph files in :

    plugins/sfCaptchaPlugin/lib/jpgraph and included the file :

    require_once ‘jpgraph/src/jpgraph_antispam-digits.php’;

    second, if you copy and paste the files from this post the single and double quotes are different characters: ´ != ‘
    and “ != ”

    It may helps to someone bye

  7. James Says:

    I am using the plugin, and have inconsistant results on whether it will show the image or not. Has anyone else had this problem? Is there some limit to the creation of the image? For example when I first load the page, I may not see the image, if I refresh the page, it comes up maybe 8 times out of 10.

  8. Marc Says:

    I also have inconsistent results, in my 2 PC all is ok in different browsers and different OS’s, but somebody else has a problem that they don’t show on the same browsers/OS’s on some other PC. Anybody have any idea why???

  9. M.G. Says:


    I followed all the instructions, but for some reason, the image doesnt show. There aren’t any errors, so I’m sure all the classes are correctly added, etc.. Does anyone have any ideas?

  10. JillElaine Says:

    If, in yrapp/config/security.yml, you have ‘all: is_secure: on’, captcha will not show.

    To fix, go to plugins/yrCaptchaplugin/modules/yrCaptcha and add directory ‘config’, then create ‘security.yml’ inside with ‘all: is_secure: off’

    My example with sfCrytographpPlugin:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: