مكوّن التحقق من الرمز
عنوان المشروع https://github.com/webman-php/captcha
التثبيت
composer require webman/captcha
الاستخدام
إنشاء ملف app/controller/LoginController.php
<?php
namespace app\controller;
use support\Request;
use Webman\Captcha\CaptchaBuilder;
class LoginController
{
/**
* صفحة الاختبار
*/
public function index(Request $request)
{
return view('login/index');
}
/**
* إخراج صورة الرمز
*/
public function captcha(Request $request)
{
// تهيئة فئة الرمز
$builder = new CaptchaBuilder;
// توليد الرمز
$builder->build();
// تخزين قيمة الرمز في الجلسة
$request->session()->set('captcha', strtolower($builder->getPhrase()));
// الحصول على بيانات الصورة الثنائية للرمز
$img_content = $builder->get();
// إخراج بيانات الصورة الثنائية للرمز
return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
}
/**
* التحقق من الرمز
*/
public function check(Request $request)
{
// الحصول على حقل captcha من الطلب POST
$captcha = $request->post('captcha');
// مقارنة قيمة captcha الموجودة في الجلسة
if (strtolower($captcha) !== $request->session()->get('captcha')) {
return json(['code' => 400, 'msg' => 'الرسم المدخل غير صحيح']);
}
return json(['code' => 0, 'msg' => 'حسناً']);
}
}
إنشاء ملف القالب app/view/login/index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>اختبار الرمز</title>
</head>
<body>
<form method="post" action="/login/check">
<img src="/login/captcha" /><br>
<input type="text" name="captcha" />
<input type="submit" value="إرسال" />
</form>
</body>
</html>
قم بالدخول إلى الصفحة http://127.0.0.1:8787/login
حيث تكون الواجهة شبيهة بما يلي:
إعدادات المعلمات الشائعة
/**
* إخراج صورة الرمز
*/
public function captcha(Request $request)
{
$builder = new PhraseBuilder(4, 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ');
$captcha = new Captcha(null, $builder);
$captcha->build();
$request->session()->set('join', strtolower($captcha->getPhrase()));
$img_content = $captcha->get();
return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
}
لمزيد من الواجهات والمعلمات، راجع https://github.com/webman-php/captcha