mardi 28 mars 2017

How to do captcha right in Laravel 5?

I am using a Captcha builder (link) for putting Captcha in my form. The code is put in the session for later comparison.

public function getForm() {
    $builder = new CaptchaBuilder;
    $builder->build();
    session(['captcha' => $builder->getPhrase()]);
    return view('form.index', array(
        'captcha' => $builder
    ));
}

public function postForm(Request $request) {
    $captcha_phase = $request->session()->get('captcha');

    if ( !empty($captcha_phase) && $inputs['captcha'] !== $captcha_phase )
        return back()->with('message', 'Wrong Captcha!')->withInput();
    //Some more other code
}

It works on some page, but for some page, I got the problem that the page is loaded twice (reference to this problem). The first request got displayed on screen while the captcha is refreshed during the second call. This make the call always wrong. While I have no way to solve the problem that the page is loaded twice, is there other ways to store the captcha so it won't be affected by the second load?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire