CaptchaBundle icon indicating copy to clipboard operation
CaptchaBundle copied to clipboard

Getting 428 response and no digits in the captcha

Open pascalrobert opened this issue 7 years ago • 6 comments

Hi,

We have a Symfony 3 app that have a captcha on a page. It works on a developer's workstation, but not on mine. When the captcha should be generated, I get:

Failed to load resource: the server responded with a status of 428 (Precondition Required)
http://localhost:8000/generate-captcha/gcb_captcha?n=13606794422fa6e0d9847c4d227ea896

The captcha is built like this:

        $form = $this->createFormBuilder()
            ->add('captcha', CaptchaType::class,[
                'width' => 318,
                'height' => 70,
                'as_url' =>true,
                'reload' =>true,
                'invalid_message' => $tr->trans('Ce champ est obligatoire.'),
                'attr'=>['oninvalid'=>""],
                'required' => false
            ])
            ->getForm();

        $form->handleRequest($request);
       $variables['form'] = $form->createView();

What is missing? I have the latest version of Captcha and CapthaBundle. PHP 7.1, with GD support. No error in the logs.

pascalrobert avatar Oct 26 '17 18:10 pascalrobert

Hello, First, be sure that the sessions are enabled in your configuration

The controller that generates the captcha is handling another requests, the field options are transported using the session, and retrieved in the generator controller. The 428 error indicates that the controller can't retrieve valid options for generation from the session and is done here:

https://github.com/Gregwar/CaptchaBundle/blob/master/Controller/CaptchaController.php#L32

The session is filled by the Captcha Genrator when the form is issued. As a side effect, loading directly the URL of the captcha without running the form will result in 428

Maybe you can inspect the contents of the session and check what it contains at this time

Gregwar avatar Nov 03 '17 11:11 Gregwar

Hi,

The session is not empty.

print_r($options, true) gives:

Array ( [charset] => 2054353 [length] => 6 [width] => 130 [height] => 50 [font] => /Users/xxxxx/Code/xxxx-web/xxxxx/vendor/gregwar/captcha-bundle/DependencyInjection/../Generator/Font/captcha.ttf [keep_value] => [as_file] => [as_url] => [reload] => [image_folder] => captcha [web_path] => /Users/xxxxx/Code/xxxxx-web/xxxx/app/../web [gc_freq] => 100 [expiration] => 60 [quality] => 30 [invalid_message] => Bad code value [bypass_code] => [whitelist_key] => captcha_whitelist_key [humanity] => 0 [distortion] => 1 [max_front_lines] => [max_behind_lines] => [interpolation] => 1 [text_color] => Array ( ) [background_color] => Array ( ) [background_images] => Array ( ) [disabled] => [ignore_all_effects] => ) [] print_r($session->has($whitelistKey), true) gives:

[2017-11-08 19:05:13] app.ERROR: []

pascalrobert avatar Nov 08 '17 19:11 pascalrobert

Looking at the session data in Symfony Profiler, I do see captcha_whitelist_key:

captcha_whitelist_key | [0 => gcb_captcha]

The printout of $session->has($whitelistKey) in my previous comment is from CaptchaController, so it looks like captcha_whitelist_key is not part of the session when coming into CaptchaController.generateCaptchaAction

pascalrobert avatar Nov 10 '17 19:11 pascalrobert

The problem was with... framework->session->cookie_secure: true. Had to set it to false.

pascalrobert avatar Nov 13 '17 21:11 pascalrobert

The problem was with... framework->session->cookie_secure: true. Had to set it to false.

Yes .. this has helped us too. Thanks..

vipinbose avatar May 23 '21 17:05 vipinbose

I have the same problem. But I'm trying to change the same that you changed, but it doesn't solve my problem. Do you have any idea? @pascalrobert and @Gregwar thanks

HishamHuneidie avatar Sep 05 '22 09:09 HishamHuneidie