Menambahkan Captcha Pada Laravel 5.1
Captcha pada sebagian besar aplikasi web merupakan hal yang diperlukan untuk menghindari web kita diakses oleh robot. Pada laravel sebenarnya banyak paket captcha untuk laravel yang bisa diintegrasikan, ada yang sangat simpel tapi ada juga yang berbayar. Dari beberapa paket captcha untuk laravel saya akhirnya memilih laravel-captcha yang dimaintain oleh bonecms. Menurut saya sesuai dengan yang saya inginkan dan simpel untuk diimplementasikan.
1. Tambahkan pada file composer.json
{
"require": {
"bonecms/laravel-captcha": "1.*"
},
}2. Lalu edit file config/app.php untuk menambahkan provider :
'providers' => [
...
"LaravelCaptcha\Providers\LaravelCaptchaServiceProvider"
],3. Jalankan perintah composer update
4. Integrasikan ke dalam controller Anda.
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use LaravelCaptcha\Lib\Captcha;
class MyController extends Controller {
public function getExample()
{
return view('myView', ['captcha' => (new Captcha)->html()]);
}
}5. Lalu untuk menampilkan capctha pada view :
...
{!! $captcha !!}
<input type="text" id="captcha" name="captcha">
...6. Lalu untuk validasi pada method post :
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
use LaravelCaptcha\Lib\Captcha;
class MyController extends Controller {
public function getExample()
{
return view('myView', ['captcha' => (new Captcha)->html()]);
}
public function postExample()
{
$code = Request::input('captcha');
if ((new Captcha)->validate($code)) {
// Validation passed
}
else {
// Validation failed
}
}
}Dengan langkah-langkah diatas, kita sudah bisa menjalankan fungsi captcha semestinya. Namun kita bisa melakukan kustomisasi lebih lanjut dengan membuat file captcha.php didalam direktori config
dengan isi konfigurasi seperti ini :
<?php
return [
/*
|--------------------------------------------------------------------------
| Font
|--------------------------------------------------------------------------
| Supported: "DroidSerif".
|
*/
'font' => 'DroidSerif',
/*
|--------------------------------------------------------------------------
| Font size
|--------------------------------------------------------------------------
| Font size in pixels.
|
|
*/
'fontSize' => 26,
/*
|--------------------------------------------------------------------------
| Letter spacing
|--------------------------------------------------------------------------
| Spacing between letters in pixels.
|
*/
'letterSpacing' => 2,
/*
|--------------------------------------------------------------------------
| Code Length
|--------------------------------------------------------------------------
| You can specify an array or integer.
|
*/
'length' => [4, 5],
/*
|--------------------------------------------------------------------------
| Displayed chars
|--------------------------------------------------------------------------
| Enter the different characters.
|
*/
'chars' => 'QSFHTRPAJKLMZXCVBNabdefhxktyzj23456789',
/*
|--------------------------------------------------------------------------
| Image Size
|--------------------------------------------------------------------------
| Captcha image size can be controlled by setting the width
| and height properties.
|
|
*/
'width' => 180,
'height' => 50,
/*
|--------------------------------------------------------------------------
| Background Captcha
|--------------------------------------------------------------------------
| You can specify an array or string.
|
*/
'background' => 'f2f2f2',
/*
|--------------------------------------------------------------------------
| Colors characters
|--------------------------------------------------------------------------
| You can specify an array or string.
|
*/
'colors' => '2980b9',
/*
|--------------------------------------------------------------------------
| Scratches
|--------------------------------------------------------------------------
| The number of scratches displayed in the Captcha.
|
*/
'scratches' => 30,
/*
|--------------------------------------------------------------------------
| Captcha style
|--------------------------------------------------------------------------
| Supported: "wave".
|
*/
'style' => 'wave',
/*
|--------------------------------------------------------------------------
| Id of the Captcha code input textbox
|--------------------------------------------------------------------------
| After updating the Captcha focus will be set on an element with this id.
|
*/
'inputId' => 'captcha',
];Bacaan terkait
Cara Install Composer di Windows Dengan Mudah
Composer adalah dependency manager yang sangat populer untuk pengembangan aplikasi berbasis PHP. Di artikel ini, Anda akan belajar cara menginstal Composer di Window...
Panduan Instalasi PHP, MySQL, dan Apache di macOS
Instalasi PHP, MySQL, dan Apache (sering disebut sebagai stack LAMP - Linux, Apache, MySQL, PHP) pada macOS sangat penting bagi pengembang web yang ingin menjalankan...
Menerapkan IP Blocker di Laravel
Untuk meningkatkan security di website, salah satunya dengan menerapkan IP blocker atau mem-blokir IP address yang mengakses website. Di laravel salah satunya dengan...
Komentar (0)
Belum ada komentar. Jadilah yang pertama.
Tulis komentar
Komentar akan dimoderasi sebelum tampil.