Laravel Session - Public for Controller

Asked

Viewed 993 times

3

I’m making a form that contains a Captcha. The Captcha is generated by a PHP script that mounts an image.

And I put the script in the folder public of Laravel 4.

When the code of Captcha is generated is logged:

session_start();
$_SESSION['numCaptcha'] = $code;

After sending the form data, send to a Controller, that has the function:

public function enviaContato() {
   echo $_SESSION['numCaptcha'];
   die();

   # Verificação de Captcha
   if (empty(Session::get('numCaptcha')) || strcasecmp(Session::get('numCaptcha'), Input::get('numCaptcha')) != 0) {
      Session::flash('error', "Captcha Inválido");
   }
}

This way that I did above is not getting the value of the session. I’ve also done:

Session::get('numCaptcha')

But it didn’t work either.

Ecziste some restriction ?
You can do it another way ?

So, how to make use of the public to the Controller.

  • 1

    You don’t think it’s better to use a captcha library Laravel ?

  • Could be. Got some up his sleeve ?

  • 1

    I used that for the Laravel 4. It uses the Sept of the Self and the validation. It is good to always try to use the maximum (or all) that is resource of the Laravel. https://github.com/mewebstudio/captcha/tree/master-l4

  • 1

    To generate the image just do it in HTML {{ Captcha::img() }}. I like it a lot! I use it : http://pbh.portaltmt.com.br

  • Yeah. I’m aware of that. But the Laravel version of this site that I’m updating is 4.0. It’s even scary to update the composer.json and give problem on the site.

  • 1

    Yeah. If it was at least the Laravel 4.2. Believe me: From 4.0 to the 4.2 there are some features that I already lack (if I were using the 4.0)

  • It doesn’t hurt to try.

  • No way. Didn’t work in this version. The requested package Mews/captcha could not be found in any version, there may be a typo in the package name.

Show 3 more comments

1 answer

2


First, you should check the configuration of the Laravel. Laravel (like most frameworks), does not use the native PHP session. I can’t remember if there’s any way to set this up.

Thus, the sessions of $_SESSION and Session::get are saved in different mechanisms (PHP saved in its default form, and Laravel saved in a file inside the folder app/storage).

So either your code has to be transformed into "pure php" or "pure Laravel".

You mentioned in the comments that you could not install the desired library. So here’s a captcha "in hand" (an "almost pure" solution) that I used one of our system in Laravel - Might be useful in your case.

Route::get('captcha', function()
{

    $word = [0 => null, 1 => null];

    for ($i = 0; $i < 4; $i++) {
        $word[0] .= chr(mt_rand(97, 122));
        $word[1] .= chr(mt_rand(97, 122));
    }

    $word = implode(' ', $word);


    Session::put('captcha_word', $word);

    $font = public_path('recaptcha/fonts/recaptchaFont.ttf');

    $image = imagecreatetruecolor(172, 50);

    $color = imagecolorallocate($image, 0, 0, 0);
    $white = imagecolorallocate($image, 255, 255, 255);

    imagefilledrectangle($image, 0, 0, 172, 99, $white);
    imagettftext($image, 22, 0, 5, 35, $color, $font, Session::get('capcha_word'));

    $tempCapcha = tempnam(null, 'captcha');

    imagepng($image, $tempCapcha);

    return Response::make(File::get($tempCapcha), 200, ['Content-Type' => 'image/png']);
});

The verification would be as follows:

if (Session::get('captcha_word') == Input::get('algum_input_com_captcha'))
{

}

And in HTML, to display Captcha, you could do it like this:

<img src="{{ URL::to('captcha') }}?{{ Str::random(8) }}" />

Observing: Note that you need to save the source used by Captcha on path specified on the route.

Updating: The passage from the variable $word can be changed to make the code cleaner, using only one line to declare it.

Thus:

$word = trim(chunk_split(Str::random(8), 4, ' '));
  • It worked. One note only is the names of your session variables are different in the code. I don’t know if you changed to post here. hood and captcha.

  • No, it is our code that is wrong. I will change in my system. I have a habit of writing captcha wrong

Browser other questions tagged

You are not signed in. Login or sign up in order to post.