Assalomu alaykum. Bugun o`zim uchun foydali deb bilgan ancha qiziqarli Google Recaptcha ni ulash haqida maqola qilishga qaror qildim.
Ho`sh v3 va v2 farqlarini googledan topsangiz bo`ladi. Asosiy farqi v2 doim biz ko`rib o`rgangan mototsiklni belgilang deydigani v2.
v3 versiya esa k`orinmaydi va u foydalanuvchining harakatiga qarab aniqlanadi va googledan shunga qarab o`tadi yoki o`tmaydi.
Endi kod qismiga kelamiz.
Googledan Recaptcha key va secretlarni olish uchun yaratish zarur. Buning uchun Google Recaptcha saytida kirasiz va kerakli maydonlarni to`ldirib saqlaysiz.
Shundan so`ng sizga Sizga Key va Secret beradi ularni olib env fayiliga saqlaymiz.
GOOGLE_RECAPTCHA_KEY=BERILGAN_KEY
GOOGLE_RECAPTCHA_SECRET=BERILGAN_SECRET
Bundan so'ng config/services.php faylining eng tagiga qo`shamiz
'recaptcha' => [
'key' => env('GOOGLE_RECAPTCHA_KEY'),
'secret' => env('GOOGLE_RECAPTCHA_SECRET'),
'url' => env('GOOGLE_RECAPTCHA_URL', "https://www.google.com/recaptcha/api/siteverify"),
],
Providers/AppServiceProvider.php faylining boot nomli funksiyasi tagiga qo'shamiz
Validator::extend('recaptchaV3', 'App\\Services\\Admin\\AdminService@checkGoogleRecaptchaV3');
app papkani ichida Services degan papka ochib uning ichida Admin nomli yana papka ochamiz Admin nomli papka ichida AdminService.php faylini ochamiz va pasdagi kodlarni tashlaymiz.
<?php
namespace App\Services\Admin;
class AdminService
{
public function checkGoogleRecaptchaV3($name): bool
{
$response = request($name);
$result = file_get_contents(
config('services.recaptcha.url'),
false,
stream_context_create([
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query([
'secret' => config('services.recaptcha.secret'),
'response' => $response,
'remoteip' => request()->ip(),
]),
],
])
);
try {
$resultJson = json_decode($result, false, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
return false;
}
if (!$resultJson->success || $resultJson->score < 0.3) {
return false;
}
return $resultJson->success;
}
}
Men Laravel UIdan foydalanganman siz o'zizngiz Login qilish malumotlari keladigan Controllerga yozasiz.
public function validateLogin(Request $request): void
{
$request->validate([
$this->username() => 'required|string',
'password' => 'required|string',
'recaptcha' => 'required|recaptchaV3',
]);
}
Bu yerda 'recaptcha' => 'required|recaptchaV3' shu recaptchani validatsiya qilish uchun AdminService dagi checkGoogleRecaptchaV3 dan tekshiradi agar validatsiyadan o`tsa bajarilishiga qo`yib beradi o'tmasa xatolik beradi.
Endi blade fayllarni to'g'rlaymiz. resources/views/auth/login.blade.php fayiliga kirib form tegining @csrf ni tagiga yozamiz
<input type="hidden" name="recaptcha" id="recaptcha">
@if (count($errors) > 0)
<div class="alert alert-danger" role="alert">
@foreach ($errors->all() as $error)
{{ $error }}<br>
@endforeach
</div>
@endif
Asosiy bladega o`tib bu kodlarni ham qo`shamiz.
<script src="https://www.google.com/recaptcha/api.js?render={{ config('services.recaptcha.key') }}"></script>
<script>
grecaptcha.ready(function () {
grecaptcha.execute('{{ config('services.recaptcha.key') }}', {action: 'contact'}).then(function (token) {
let recaptchaId = document.getElementById('recaptcha');
if (token && recaptchaId) {
recaptchaId.value = token;
}
});
});
</script>
Va bo'ldi endi ishlashi kerak. Agarda sizda biror muammo bolsa Telegram Guruhimizda so'rang.
Maqola UzHackerSW.uz sayti uchun maxsus tayyorlandi. Ruxsatsiz maqolani ko‘chirish qatiyan taqiqlanadi. Maqola o`g`rincha ko`chirilganda websaytga havola ko`rsatilishi shart!
Bundanda qiziqarli ma'lumotlarga ega bo'lish uchun saytimizga tez-tez kirib turing! Biz eng yaxshilarini sizlarga ilinamiz