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, ' '));
You don’t think it’s better to use a captcha library
Laravel
?– Wallace Maxters
Could be. Got some up his sleeve ?
– Diego Souza
I used that for the
Laravel 4
. It uses the Sept of the Self and thevalidation
. It is good to always try to use the maximum (or all) that is resource of theLaravel
. https://github.com/mewebstudio/captcha/tree/master-l4– Wallace Maxters
To generate the image just do it in HTML
{{ Captcha::img() }}
. I like it a lot! I use it : http://pbh.portaltmt.com.br– Wallace Maxters
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.– Diego Souza
Yeah. If it was at least the
Laravel 4.2
. Believe me: From4.0
to the4.2
there are some features that I already lack (if I were using the4.0
)– Wallace Maxters
It doesn’t hurt to try.
– Diego Souza
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.
– Diego Souza