कैप्चा घटक
परियोजना पते: 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 फ़ील्ड प्राप्त करें
$captcha = $request->post('captcha');
// सत्र में captcha मान की तुलना करें
if (strtolower($captcha) !== $request->session()->get('captcha')) {
return json(['code' => 400, 'msg' => 'प्रविष्ट कैप्चा सही नहीं है']);
}
return json(['code' => 0, 'msg' => 'ok']);
}
}
टेम्पलेट फ़ाइल 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